Reputation: 55
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.
Upvotes: 1
Views: 101
Reputation: 56688
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.
Upvotes: 1
Reputation: 193
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.
Upvotes: 0
Reputation: 330
Try to add after the page load using javascript or jquery
$("#<%=cboContact.ClientID%>").val("None");
Upvotes: 0
Reputation: 697
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
Upvotes: 0