这是我的aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True" EnablePersistedSelection="True">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
<asp:BoundField DataField="Section" HeaderText="Section"
SortExpression="Section" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:Button runat="server" ID="btnedit" Text="Edit" CommandName="EditRow"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Deleterow"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这里有代码:
protected void btnsub_Click(object sender, EventArgs e)
{
SqlConnection con = Connection.DBconnection();
if (Textid.Text.Trim().Length > 0)
{
SqlCommand com = new SqlCommand("StoredProcedure3", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@id", Textid.Text.Trim());
com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
com.Parameters.AddWithValue("@address", Textaddress.Text.Trim());
com.ExecuteNonQuery();
GridViewRow gr = GridView1.SelectedRow;
gr.Cells[1].Text = Textusername.Text;
gr.Cells[2].Text = Textclass.Text;
gr.Cells[3].Text = Textsection.Text;
gr.Cells[4].Text = Textaddress.Text;
}
else
{
SqlCommand com = new SqlCommand("StoredProcedure1", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
com.Parameters.AddWithValue("@address", Textaddress.Text.Trim());
com.ExecuteNonQuery();
Response.Redirect("studententry.aspx");
}
}
所选行未在gridview中选择。我可以知道如何启用gridview行。我使用msdn和其他文件,然后我跟着,但没有任何帮助。
我设计了我设置启用选择,但我仍然没有发现问题。
任何人都可以帮助我吗?
谢谢,
答案 0 :(得分:0)
基本上我们得到GridView的SelectedRow在一些GridviewRow Action事件(例如OnSelectedIndexChanged
,OnSelectedIndexChanging
,OnRowEditing
或者像Button
点击的模板控件上。但是在编码中我不认为你的btnsub_Click
在Gridview中,所以如果你想在GridviewRow Action事件后使用SelectedGridViewRow,那么将索引保存在某个临时变量中,或者将变量传递给方法作为参数
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{
int index = Gridview1.SelectedIndex;
hiddenfield.Value = index.ToString();
}
OR 在OnRowEditing中你也可以得到行的索引
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
int index = gr.RowIndex;
hidval.Value = index.ToString();
}
}
和按钮单击,您可以将隐藏字段值作为索引:
protected void btnsub_Click(object sender, EventArgs e)
{
SqlConnection con = Connection.DBconnection();
if (Textid.Text.Trim().Length > 0)
{
SqlCommand com = new SqlCommand("StoredProcedure3", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@id", Textid.Text.Trim());
com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
com.Parameters.AddWithValue("@address", Textaddress.Text.Trim());
com.ExecuteNonQuery();
if(!String.IsNullOrEmpty(hiddenfield.Value))
{
int index = Convert.ToInt16(hiddenfield.Value);
GridView1.Rows[index].Cells[1].Text = Textusername.Text;
GridView1.Rows[index].Cells[2].Text = Textclass.Text;
GridView1.Rows[index].Cells[3].Text = Textsection.Text;
GridView1.Rows[index].Cells[4].Text = Textaddress.Text;
}
}
else
{
SqlCommand com = new SqlCommand("StoredProcedure1", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Name", Textusername.Text.Trim());
com.Parameters.AddWithValue("@Class", Textclass.Text.Trim());
com.Parameters.AddWithValue("@Section", Textsection.Text.Trim());
com.Parameters.AddWithValue("@address", Textaddress.Text.Trim());
com.ExecuteNonQuery();
Response.Redirect("studententry.aspx");
}
}