【发布时间】:2016-04-20 23:51:43
【问题描述】:
我在页面上有几个 GridView,它们都有问题。当 OnRowDeleting 函数被触发时,GridView(和发件人)找不到任何行。我通过以下方式创建了 GridView:
<asp:GridView ID="gridview9" runat="server"
DataKeyNames="id"
OnRowDeleting="deleteNumberFrom9"
AutoGenerateColumns="false"
CssClass="mGrid"
AlternatingRowStyle="alt">
<Columns>
<asp:boundfield datafield="product_key" headertext="PRODUCT KEY" ItemStyle-HorizontalAlign="Center" />
<asp:boundfield datafield="first_name" headertext="FIRST NAME"/>
<asp:boundfield datafield="last_name" headertext="LAST NAME"/>
<asp:boundfield datafield="email" headertext="EMAIL"/>
<asp:boundfield datafield="phone_number" headertext="PHONE NUMBER" ItemStyle-HorizontalAlign="Center" />
<asp:CommandField HeaderText="DELETE NUMBER" ShowDeleteButton="True" ItemStyle-HorizontalAlign="Center" DeleteText="Delete" />
</Columns>
</asp:GridView>
网格由 MySQL 数据库填充:
GridView gv = gridview9;
String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionStringName].ToString();
MySqlConnection connection = new MySqlConnection(connectionString);
connection.Open();
MySqlCommand command = new MySqlCommand(queryString, connection);
if (addPhone) command.Parameters.AddWithValue("@phone_number", "%" + phoneNumber + "%");
if (addEmail) command.Parameters.AddWithValue("@email", "%" + email + "%");
if (addFirstName) command.Parameters.AddWithValue("@first_name", "%" + firstName + "%");
if (addLastName) command.Parameters.AddWithValue("@last_name", "%" + lastName + "%");
if (addProductKey) command.Parameters.AddWithValue("@product_key", "%" + productKey + "%");
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
gv.DataSource = ds;
gv.DataBind();
gv.Visible = true;
最后,onRowDeleting函数是:
protected void deleteNumberFrom9(object sender, GridViewDeleteEventArgs e)
{
int SID = Convert.ToInt32(gridview9.DataKeys[e.RowIndex].Value);
String phone_number = e.Values[4].ToString();
deleteNumber(SID, phone_number, 9);
}
当我运行页面时,gridview9 正确返回 2 行,但是当我单击其中一行中的删除并且 deleteNumberFrom9 被触发时,gridview9.Rows.Count 是 0 而不是 2。当我将发件人添加到监视列表并查看行数也是0。
【问题讨论】: