Reputation:
Dropdown list is bound dynamically as:
<asp:DropDownList ID="ddlSource" class="ddl" runat="server" DataSourceID="FROM1" DataTextField="CompanyName" DataValueField="CompanyName">
</asp:DropDownList>
<asp:SqlDataSource ID="FROM1" runat="server" ConnectionString="<%$ ConnectionStrings:PIMSConnectionString2 %>" SelectCommand="SELECT [CompanyName] FROM Company ORDER BY [CompanyName]"></asp:SqlDataSource>
<br />
The CompanyName
table has a value that is equal to "Select" and I want this value to be shown in the dropdown list but should be grayed out (disabled) so users can't select or set it.
Any idea how to do it in ASP.NET (not HTML)?
Upvotes: 1
Views: 3797
Reputation: 4312
There is complete answer based on other questions from comments.
Make ListItems
disabled and put them on the top of ddlSource
based on condition CompanyName="Select"
:
aspx :
<asp:DropDownList ID="ddlSource" class="ddl" runat="server" > </asp:DropDownList>
<asp:SqlDataSource ID="FROM1" runat="server" ConnectionString="<%$ ConnectionStrings:PIMSConnectionString2 %>" SelectCommand=""></asp:SqlDataSource>
<br />
code behind :
public void PopDDL()
{
FORM1.SelectCommand = "SELECT CompanyName FROM Company ORDER BY CASE WHEN CompanyName='Select' THEN 0 ELSE 1 END;"
ddlSource.DataSourceID = "FORM1";
ddlSource.DataTextField = "CompanyName";
ddlSource.DataValueField = "CompanyName";
ddlSource.DataBind();
foreach (ListItem itm in ddlSource.Items) {
if (itm.Value == "Select") {
itm.Attributes.Add("disabled", "disabled");
}
}
}
protected void Page_Load(object sender, System.EventArgs e)
{
PopDDL();
}
Upvotes: 1
Reputation: 4312
Try this, but populating ddlSource
will be from code behind (it's in vb.net, sorry) :
Public Sub PopDDL()
FORM1.SelectCommand = "SELECT CompanyName FROM Company ORDER BY CompanyName;"
ddlSource.DataSourceID = "FORM1"
ddlSource.DataTextField = "CompanyName"
ddlSource.DataValueField = "CompanyName"
ddlSource.DataBind()
For Each itm As ListItem In ddlSource.Items
If itm.Value = *your_condition* Then 'condition why this item must be disabled
itm.Attributes.Add("disabled", "disabled")
End If
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
PopDDL()
End Sub
Item will be displayed but disabled (gray).
Update : I use conversion tool (vb.net to c#) and I think this is c# code :
foreach (ListItem itm in ddlSource.Items) {
if (itm.Value == *your_contidion*) { 'condition why this item must be disabled
itm.Attributes.Add("disabled", "disabled");
}
}
If You use itm.Enabled = False
, item will not be showed.
UPDATE #2 (converted to c# code by online converter) :
aspx page :
<asp:DropDownList ID="ddlSource" class="ddl" runat="server" > </asp:DropDownList>
<asp:SqlDataSource ID="FROM1" runat="server" ConnectionString="<%$ ConnectionStrings:PIMSConnectionString2 %>" SelectCommand=""></asp:SqlDataSource>
<br />
code behind :
public void PopDDL()
{
FORM1.SelectCommand = "SELECT CompanyName FROM Company ORDER BY CompanyName;";
ddlSource.DataSourceID = "FORM1";
ddlSource.DataTextField = "CompanyName";
ddlSource.DataValueField = "CompanyName";
ddlSource.DataBind();
foreach (ListItem itm in ddlSource.Items) {
if (itm.Value == *your_condition*) {
itm.Attributes.Add("disabled", "disabled");
}
}
}
protected void Page_Load(object sender, System.EventArgs e)
{
PopDDL();
}
Upvotes: 0