1

I am very new to C# and aspx files. we are using very old application written in 2008 and don't have any contact details of developer. what we want to do is to make some changes as some parts of original code is not relevant any more. I've managed to locate .aspx file and did some changes but some of them are not working. was trying to locate .aspx.cs file but it looks like they all were compiled in .dll file in bin folder. I did find the original .aspx.cs file in back up.

The problem I have is that one of the fields in the form is dropdown list and is linked to data table in sql and is set up as required. When I open web page, the default value in this dropdown list is -Select Contact Method-.

My understanding this is because the code behind has the following:

# region populate ContactMethod Combo(Primary and Secondary)**
private void PopulateContactMethod(int intContactMethodID)
{
    // get data
    MasterValue oMV = new MasterValue();
    DataTable dt = oMV.GetAll(MasterValueType.ContactMethod);
    // populate combo
    oUtil.PopulateCombo(cboContact, dt, "intID", "strText",
    intContactMethodID.ToString(), "-Select Contact Method-");
}
# endregion

The SQL table has the following values 1=Home Phone, 2= Mobile, 3=Email & 4 =None

And the .aspx file has the following:

<%@ Page CodeBehind="add_new_user.aspx.cs" Language="c#" AutoEventWireup="false"
 Inherits="App.UI.add_new_user" %>
.
.
.
<tr>
    <td class="formtext" align="right">Primary Contact Method:<SPAN class="star">*</SPAN>
    </td>
    <td class="formtext" vAlign="top" align="left">
       <asp:dropdownlist id="cboContact" runat="server" CssClass="ListBox" Width="150px">
       </asp:dropdownlist>
       <asp:requiredfieldvalidator id="rfvContactMethod" runat="server" 
        ControlToValidate="cboContact" Display="None" ErrorMessage="Please select contact
        method" InitialValue="0">
       </asp:requiredfieldvalidator>
    </td>
</tr>

What we need is when we open web page default value in this dropdown list is None (4) and not -Select Contact Method-.

Any help will be much appreciated!

Thank you in advance.

4
  • I doubt there is a simple solution to your problem, since you do not have access to code behind. However why can't you use the code you found in the back ups and create a new solution using it and the aspx you already have? Then you can naturally edit the code behind. Commented Feb 10, 2016 at 11:32
  • Hi Andrei, I thought of this. But first of all as I am new to all of this I wouldn't know what to do as it look like there are around 1000 .aspx .aspx.cs & .aspx.resx files which all been compiled in .dll file. And secondly as the app is 9 years old and we are still using it on daily basis I don't want to break it. So trying to find solutions around. Thanks Commented Feb 10, 2016 at 12:21
  • Will client-side only solution work for you, javascript that is? Because without modifying code behind what you want is just impossible Commented Feb 10, 2016 at 14:18
  • Hi Andrei, I guess this would work. What I need is that when someone adds a new user using this web form, they don't have to select this field. Commented Feb 15, 2016 at 15:18

4 Answers 4

1

As far as I can see this .aspx page does not use a master page. If so, locate the <head> tag and add a new <script> to it. Inside it there will be a function to fix selection of the list:

<head>
    ...
    <script type="text/javascript">
      function fixContactListSelection() {
          var list = document.getElementById("<%=cboContact.ClientID%>");
          list.value = '4';
      }

      if (window.addEventListener) {
          window.addEventListener('load', fixContactListSelection, false);
      } else if (window.attachEvent) {
          window.attachEvent('onload', fixContactListSelection);
      }
    </script>
</head>

If however there is a master page used, and you cannot find a head tag, insert this <script> with its content pretty much anywhere in the page. Right under the DDL declaration should be fine.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Andrei, Tried doing this, still when I open the form the default value in this dropdown list is -Select Contact Method- :-(
@Yuri, just spotted a bug in my script code, please update. Also, do you see any javascript errors on the page?
@ Andrei, Thank you so much! You are a star it finally worked!
@ Andrei, is it possible to modify this code so that it only selects this value wit it is blank or -Select Contact Method-? The reason for this is that the aspx file is used for new users and existing user. So if I open existing user for, lets say to update their address or contact number, the contact method field automatically changes to None. Don't worry if it is too complicated to do this.
0

try to bind the dropdownlist like this

 private void PopulateContactMethod(int intContactMethodID)
    {
    DataTable dt = new DataTable();
            dt = "get Your data from db";

            cboContact.DataSource = dt;
            cboContact.DataTextField = dt.Columns["field to view"].ToString();
            cboContact.DataValueField = dt.Columns["id of field to view"].ToString();
            cboContact.DataBind();
            ListItem li1 = new ListItem("--Select Contact Method--", "0");
            cboContact.Items.Insert(0, li1);
    }

the line

ListItem li1 = new ListItem("--Select Contact Method--", "0");
                cboContact.Items.Insert(0, li1);

makes the "--Select Contact Method--" as default value

3 Comments

The same as above, if this needs to go in .aspx.cs then I cant do this. And I don't want to have "--Select Contact Method--" as default value I want "None" as default value. Thanks
you said you cant access the .aspx.csfile right?try this link.it explains how to embed code blocks using server tags aspsnippets.com/Articles/…
Hi Sruthi, thanks for the link. I had a look but I wouldn't know how I could use in my aspx file
0

Try to add after the page load using javascript or jquery

 $("#<%=cboContact.ClientID%>").val("None");

5 Comments

Sorry as mentioned above I am new to coding. Dose this go in .aspx file? If so where? If it goes to .aspx.cs then I cant edit this as it been compiled in .dll file in bin folder
youcan add this on your .aspx.cs file,and call the method PopulateContactMethod(int intContactMethodID) from where you need to bind the dropdownlist
I don't have .apx.cs file all I have is .dll file
In that case add the following in the aspx page itself as follows <asp:dropdownlist id="cboContact" runat="server" CssClass="ListBox" Width="150px"> <asp:ListItem Value="4">None</asp:ListItem> <asp:ListItem Selected="True"></asp:ListItem> </asp:dropdownlist>
Hi Ram, Tried doing this still when I open the page the default value in the field is still "--Select Contact Method--". I did try similar yesterday when researching the web.
0

Please check below

MasterValue oMV = new MasterValue();
DataTable dt = oMV.GetAll(MasterValueType.ContactMethod);
//Now you assign specific field name and field id like below
cboContact.DataSource = dt;
cboContact.DataTextField = "strText";
cboContact.DataValueField = "intID";
cboContact.DataBind();

It will resolve your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.