[英]How do I get TextBox in a GridView row when Edit button is clicked if the headers are not databound?
[英]how to select row when an edit button in gridview
我已經創建了具有編輯、更新和刪除功能的 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 是真的。 當我單擊編輯按鈕時,在放置斷點后它顯示空值,這意味着所選行不起作用。 這是我的輸出截圖
誰能幫我解決這個問題?
謝謝,
您可以按照以下三種方法在按鈕單擊時突出顯示當前行。
方法一:
在 gridview 標記和代碼隱藏中添加OnSelectedIndexChanged
事件。
GridView 控件標記
<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 中循環,直到獲得選定的行索引。 這會降低性能。
方法二:
下面的方法比以前的方法要好得多。 在這里,您將不需要循環,只需在按鈕單擊事件下提及以下內容即可完成。 我沒有在 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");
}
方法三:
由於您使用的是 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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.