NewbieCoder NewbieCoder - 1 month ago 11
ASP.NET (C#) Question

Server tag not well formed for ASP DropDownList for ItemTemplate

<asp:TemplateField HeaderText="Company Membership">
<ItemTemplate>
<asp:DropDownList id="ddlCompanyMembership" runat="server" class="form-control" onchange="verifyUser('<%# ((DataRowView)Container.DataItem)["CCMP_CODE"].ToString() %>');" />
</ItemTemplate>
</asp:TemplateField>


I can't seem to build this properly. It's suppose to call a Javascript function and pass the value to the function. But it shows
Server tag not well formed
error.

I've tried looking for solutions, some said the quotes and double quotes do matter. But I've tried everything and nothing's working.

Answer

You may consider binding onchange event on the server side by handling GridView's RowDataBound event. You will be able to bind your event as follows which is more convenient in my opinion.

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e) 
{
   if (e.Row.RowType == DataControlRowType.DataRow) 
   {
       // Find your drop down list
       DropDownList ddl = (DropDownList)e.Row.FindControl("ddlCompanyMembership");

       // Add onchange event as attribute
       ddl.Attributes["onchange"] = "verifyUser('your logic');";
   }
}