我有一个带有 arowcommand
和 a的网格视图linkbutton
。当我按下链接按钮时,行被编辑但是 DDL 应该将数据与我的数据库表中的值绑定,但这不起作用
我不知道这是否是正确的方法,但是在按下链接按钮“对象引用未设置为对象的实例”时确实出现错误。但我不认为这是这里的主要问题。
从 Rowcommand 编辑时我应该绑定我的 DDL 行还是有人能看到我做错了什么?
DDL 列的 ASPX 标记:
<asp:TemplateField HeaderText="Status">
<EditItemTemplate>
<asp:DropDownList ID="ddlStatusRow" AppendDataBoundItems="True" runat="server" >
<asp:ListItem>Status</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfEditStatus" runat="server" ErrorMessage ="Status is a required field"
ControlToValidate="ddlStatusRow" Text="*" ForColor="Red" InitialValue="Status">
</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle BackColor="#E6E6E6" Font-Bold="False" Font-Names="Arial" Font-Size="14px" ForeColor="Black" />
<ItemStyle Font-Size="12px" HorizontalAlign="Center" Width="4%" />
</asp:TemplateField>
代码隐藏:
protected void gwUnplannedActivities_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRow")
{
int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
gwUnplannedActivities.EditIndex = rowIndex;
BindGridViewUnplannedActivities();
GridViewRow row = ((e.CommandSource as Control).NamingContainer as GridViewRow);
DropDownList ddlStatusRow = (DropDownList)row.Cells[3].FindControl("ddlStatusRow");
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM ....", con);
con.Open();
ddlStatusRow.DataSource = cmd.ExecuteReader();
ddlStatusRow.DataTextField = "Status";
ddlStatusRow.DataBind();
}
}
else if (e.CommandName == "CancelUpdate")
{
//somecode....
}
else if (e.CommandName == "UpdateRow")
{
//somecode....
}
}