문제

I am using a gridview which is having dropdownlist in edittemplate field. There are 3 list items in dropdown : Red,Amber,Green. Instead of displaying text in listitems, I want to show the colors, for the same I am using dropdownlist's onLoad event, however this event is not able to recognize the dropdownlist. Dropdownlist Designer code :

<asp:TemplateField HeaderText="Color">
<EditItemTemplate> 
  <asp:DropDownList ID="ddlcolor" runat="server" AppendDataBoundItems="true" DataTextField="COLOR" DataValueField ="COLOR" OnLoad="DDLColor_Load">
            <asp:ListItem Value="-1">- Select Color -</asp:ListItem>
            <asp:ListItem Value="0">Amber</asp:ListItem>
            <asp:ListItem Value="1">Green</asp:ListItem>
            <asp:ListItem Value="2">Red</asp:ListItem>                
  </asp:DropDownList></EditItemTemplate></asp:TemplateField>

Dropdownlist onLoad Event in codebehing :

  protected void DDLColor_Load(object sender, EventArgs e)
{

    for (int i = 0; i < ddlcolor.Items.Count; i++)
    {
        ddlcolr.Items[i].Attributes.Add("style", "background-color:" + ddlcolor.Items[i].Text);

    }
} 

However, it shows that the dropdownlist ddlcolor does not exists in current context. Do I need to find this control in gridview ? Please suggest.

도움이 되었습니까?

해결책

You are right. You need to find the control. Using the sender argument will help you locate the drop down without using the find control method

 protected void DDLColor_Load(object sender, EventArgs e)
 {
    DropdownList ddlcolr=(Dropdownlist)sender;
for (int i = 0; i < ddlcolor.Items.Count; i++)
  {
    ddlcolr.Items[i].Attributes.Add("style", "background-color:" + ddlcolor.Items[i].Text);

  }
 } 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top