[英]How to find control in ControlParameter? asp.net c#
我使用EditItemTemplate在GridView中创建DropDownList以便在数据库中编辑数据,但是当我选择值并单击“编辑”按钮时,无法从DropDownList获得值。
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
SqlDataAdapter dtAdapter;
DataTable dt = new DataTable();
strSQL2 = "SELECT distinct(gp.RespIndexID) RespIndexID,gr.RespDesc " +
"FROM Login as gbl " +
"LEFT JOIN Profile gp ON gbl.EmpID=gp.EmpID " +
"LEFT JOIN ResponeIndex gr ON gp.RespIndexID=gr.IndexID " +
"WHERE gbl.EmpDept=" + DropDownList3.SelectedValue.ToString() + " AND gbl.EmpSection=" + DropDownList4.SelectedValue.ToString() + " AND gbl.EmpUnit=" + DropDownList1.SelectedValue.ToString() + " AND gbl.Status is Null";
try
{
dtAdapter = new SqlDataAdapter(strSQL2, objConn);
dtAdapter.Fill(dt);
for (int i = 0; dt.Rows.Count > i; i++)
{
if (GridView1.EditIndex == e.Row.RowIndex)
{
DropDownList ddlPosition = (DropDownList)e.Row.Cells[3].FindControl("ddlPosition");
HiddenField HiddenField1 = (HiddenField)e.Row.Cells[3].FindControl("HiddenField1");
ddlPosition.Items.Add(new ListItem((string)dt.Rows[i]["RespIndexID"].ToString(), dt.Rows[i]["RespIndexID"].ToString()));
if (Page.IsPostBack == true)
{
ddlPosition.SelectedValue = HiddenField1.Value;
}
//er.NewValues[i] = ddlPosition.SelectedValue.ToString();
}
}
}
catch (Exception err)
{
//Response.Write(strSQL2 + "<br />" + err);
}
}
// GridView中的代码
<asp:TemplateField HeaderText="RespDesc" SortExpression="RespDesc">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("RespIndexID") %>'></asp:HiddenField>
<asp:DropDownList ID="ddlPosition" runat="server" AutoPostBack="false" AppendDataBoundItems="True"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RespIndexID") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
//数据源中的代码更新
UpdateCommand="UPDATE Profile SET RespIndexID = @ddlPosition WHERE (EmpID = @EmpID)">
<UpdateParameters>
<asp:ControlParameter ControlID="ddlPosition" Name="RespIndexID" PropertyName="SelectedValue" Type="String" />
<asp:Parameter Name="EmpID" />
首先,您应该注意,您的代码已针对SQL Injections打开,因此请从对查询进行参数化开始。
现在,您的问题-您可以通过使用FindControl方法获取DropDownList的代码在后面的代码中完成。 注册到RoweCommand事件:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
DropDownList ddl = (DropDownList)row.FindControl("ddlPosition");
// Do whatever you want with the dropdownlist
}
}
其中“编辑”是您在LinkButton中使用的命令的名称
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.