Gridview update statement
-
I ahve a gridview control on my form. In the gridview i have an Item Template like:
Yes No
I cannot understand how to capture the DropDownList value in my update statement. Any ideas? Here is current Update parameters: Thanks, Taen Karth -
I ahve a gridview control on my form. In the gridview i have an Item Template like:
Yes No
I cannot understand how to capture the DropDownList value in my update statement. Any ideas? Here is current Update parameters: Thanks, Taen KarthHi there, You can simply use the
Bind
method to do the updating with the dropdownlist control as theBind
method supports the two-way databinding. The sample will go like this:<asp:DropDownList ID="ddlComplete" runat="server" SelectedValue='<%# Bind("NComplete") %>'>
asp:ListItemYes</asp:ListItem>
<asp:ListItem Selected="True">No</asp:ListItem>
</asp:DropDownList>Another way comes to mind is to use the ControlParameter[^] declared in the
UpdateParameters
field. Basically, theControlParameter
allows you to set the parameter to the selected property of a control like the dropdownlist. So what you need to do is to specify the control id in thecontrolid
property and the selected property in thepropertyname
:<asp:controlparameter name="Description"
controlid="GridView1$ctl02$ddlComplete" propertyname="SelectedValue"/>However, you should pay a bit of your attention to the control id of the dropdownlist specified in the
controlid
property. The value of the property includes the id of the container since the dropdownlist is placed inside the GridView control. Also, the index of the selected row is dynamic, not static like 2 in the example, so you'll have to update thecontrolid
property based on the currently selected row, for example you can do that in theRowUpdating
event. -
Hi there, You can simply use the
Bind
method to do the updating with the dropdownlist control as theBind
method supports the two-way databinding. The sample will go like this:<asp:DropDownList ID="ddlComplete" runat="server" SelectedValue='<%# Bind("NComplete") %>'>
asp:ListItemYes</asp:ListItem>
<asp:ListItem Selected="True">No</asp:ListItem>
</asp:DropDownList>Another way comes to mind is to use the ControlParameter[^] declared in the
UpdateParameters
field. Basically, theControlParameter
allows you to set the parameter to the selected property of a control like the dropdownlist. So what you need to do is to specify the control id in thecontrolid
property and the selected property in thepropertyname
:<asp:controlparameter name="Description"
controlid="GridView1$ctl02$ddlComplete" propertyname="SelectedValue"/>However, you should pay a bit of your attention to the control id of the dropdownlist specified in the
controlid
property. The value of the property includes the id of the container since the dropdownlist is placed inside the GridView control. Also, the index of the selected row is dynamic, not static like 2 in the example, so you'll have to update thecontrolid
property based on the currently selected row, for example you can do that in theRowUpdating
event.Ok excellent... I was trying to do your second option yet it could not find the "controlid" of the dropdownlist as you have mentioned. However, I don't quite understand how I can update the "controlid" durning the RowUpdating event. Would it be something like:
Protected Sub Gridview1_RowUpdating(ByVal.......) Dim ddlID as String = ddlNComplete Gridview1.SelectedRow.FindControl("DropDownList").ID = ddlID End Sub
Let me know and thanks for the info ;) Thanks, Taen Karth -
Ok excellent... I was trying to do your second option yet it could not find the "controlid" of the dropdownlist as you have mentioned. However, I don't quite understand how I can update the "controlid" durning the RowUpdating event. Would it be something like:
Protected Sub Gridview1_RowUpdating(ByVal.......) Dim ddlID as String = ddlNComplete Gridview1.SelectedRow.FindControl("DropDownList").ID = ddlID End Sub
Let me know and thanks for the info ;) Thanks, Taen KarthThe reason I use the
RowUpdating
event is that this event occurs just before the parameters are populated and the GridView control updates the row. For example, the GridView control uses the SQL datsource defined as below:<asp:SqlDataSource
ID="SqlDataSource1"
Runat="Server"
UpdateCommand="..."
...<UpdateParameters>
<asp:controlparameter name="NComplete" controlid="GridView1$ctl02$ddlNComplete"
propertyname="SelectedValue"/>
...
</UpdateParameters>
</asp:SqlDataSource>The sample code for the
RowUpdating
event handler looks something like:protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
ControlParameter parameter = (ControlParameter)SqlDataSource1.UpdateParameters[0];
int index = e.RowIndex + 2;
parameter.ControlID = "GridView1$ctl0" + index + "$ddlNComplete";
}