Reputation: 1407
I can't getting the DataValueField setting on Drop Down List on Edit Item Template in c# .
I need update the row of GridView opening new aspx page in window popup where get the ID selected from DB MySQL.
I have this error :
Object reference not set to an instance of an object
in this code-behind line :
string taskID = ddlID.DataValueField;
Can you help me ?
My code below, thank you in advance for any help, really appreciated.
.cs
protected void ddlID_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlID = new DropDownList();
ddlID = (DropDownList)FindControl("ddlID");
string taskID = ddlID.DataValueField;
string queryString = "newPage.aspx?membershipId=" + taskID.ToString().ToUpper();
string newWin = "var Mleft = (screen.width/2)-(1200/2);
var Mtop = (screen.height/2)-(700/2);
window.open('" + queryString + "','_blank','height=600,width=900,
status=yes,
toolbar=no,scrollbars=yes,menubar=no,
location=no,top=\'+Mtop+\', left=\'+Mleft+\';');";
ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}
.aspx
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="ddlID" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="ddlID_SelectedIndexChanged"
BackColor="Yellow" DataValueField='<%# Eval("ID")%>'>
<asp:ListItem Text="--------------" Value=""></asp:ListItem>
<asp:ListItem Text="Update Name" Value="1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
#Edit01
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="ddlID" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="ddlID_SelectedIndexChanged"
BackColor="Yellow" DataValueField="ID">
<asp:ListItem Text="--------------" Value=""></asp:ListItem>
<asp:ListItem Text="Update Name" Value="1"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
protected void ddlID_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlID = new DropDownList();
string taskID = ddlID.SelectedValue;
string queryString = "newPage.aspx?membershipId=" + taskID.ToString().ToUpper();
string newWin = "var Mleft = (screen.width/2)-(1200/2);
var Mtop = (screen.height/2)-(700/2);
window.open('" + queryString + "','_blank','height=600,width=900,
status=yes,
toolbar=no,scrollbars=yes,menubar=no,
location=no,top=\'+Mtop+\', left=\'+Mleft+\';');";
ClientScript.RegisterStartupScript(this.GetType(), "pop", newWin, true);
}
Upvotes: 0
Views: 270
Reputation:
Your DropDownList is in EditTemplate of GridView so you have to get it from sender
:
DropDownList ddlID = sender as DropDownList;
string taskID = ddlID.DataValueField;
Upvotes: 1