我已经使用编辑,更新和删除创建了gridview。
当我选择链接时,将选择特定的行。但是当我单击编辑按钮时,所选行没有选择。
我可以知道,我该如何解决这个问题?
这是我的cs文件:
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 = Textid.Text;
gr.Cells[2].Text = Textusername.Text;
gr.Cells[3].Text = Textclass.Text;
gr.Cells[4].Text = Textsection.Text;
gr.Cells[5].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");
}
}
protected void btnrst_Click(object sender, EventArgs e)
{
Textusername.Text = null;
Textclass.Text = null;
Textsection.Text = null;
Textaddress.Text = null;
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
SqlConnection con = Connection.DBconnection();
if (e.CommandName == "EditRow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
Textid.Text = gr.Cells[1].Text;
Textusername.Text = gr.Cells[2].Text;
Textclass.Text = gr.Cells[3].Text;
Textsection.Text = gr.Cells[4].Text;
Textaddress.Text = gr.Cells[5].Text;
}
else if (e.CommandName == "Deleterow")
{
GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
SqlCommand com = new SqlCommand("StoredProcedure4", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
var id = Int32.Parse(e.CommandArgument.ToString());
GridView1.Rows[id].Visible = false;
com.ExecuteNonQuery();
}
}
这是我的aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True"
EnablePersistedSelection="True" BackColor="White">
<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>
<ControlStyle BorderColor="#CCFF66" />
</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>
<SelectedRowStyle BackColor="#FF66FF" />
</asp:GridView>
我检查了设计,我确信启用selectedrowbutton是真的。当我单击编辑按钮时,在放置断点后它显示空值,意味着,selectedrow不起作用。这是我的输出screenshot
任何人都可以帮我解决这个问题吗?
谢谢,
答案 0 :(得分:1)
您可以按照以下三种方法突出显示按钮单击时的当前行。
方法1:
在gridview标记和代码隐藏中添加OnSelectedIndexChanged
事件。
GridView Control Mark-up
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True"
EnablePersistedSelection="True" BackColor="White"
OnSelectedIndexChanged = "OnSelectedIndexChanged"
EnableViewState="true">
</asp:GridView>
<强>代码隐藏强>
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowIndex == GridView1.SelectedIndex)
{
row.BackColor = ColorTranslator.FromHtml("#A1DCF2");
}
else
{
row.BackColor = ColorTranslator.FromHtml("#FFFFFF");
}
}
}
但是在这里你将在gridview中循环,直到你得到选定的行索引。这将降低性能。
方法2:
以下方法远比前一方法好。在这里,你不需要循环只提按下按钮点击事件休息一切都将完成。我没有在IDE中测试过这个,也不能保证它成功。
Button btnEdit = Button as sender;
GridViewRow gvr = (GridViewRow)btnEdit.NamingContainer;
int rowindex = gvr.RowIndex;
if (rowindex == GridView1.SelectedIndex)
{
gvr.BackColor = ColorTranslator.FromHtml("#A1DCF2");
}
else
{
gvr.BackColor = ColorTranslator.FromHtml("#FFFFFF");
}
方法3:
由于您使用的是RowCommand,因此可以应用以下内容: -
int rowindex = Convert.ToInt32(e.CommandArgument);
GridViewRow gvr = GridView1.Rows(rowindex);
if (rowindex == GridView1.SelectedIndex)
{
gvr.BackColor = ColorTranslator.FromHtml("#A1DCF2");
}
else
{
gvr.BackColor = ColorTranslator.FromHtml("#FFFFFF");
}