NewbieCoder NewbieCoder - 9 months ago 49
ASP.NET (C#) Question

Server tag not well formed for ASP DropDownList for ItemTemplate

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

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

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 Source

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');";