我可以在gridview之外制作gridview.editindex = e.rowindex - 1更新按钮 [英] Can I make gridview.editindex = e.rowindex - 1 update button present outside of gridview
问题描述
protected void gvMain_RowEditing(object sender, GridViewEditEventArgs e)
{
}
我上面甚至是handlar,这是空的
在row_Command我收到数据并将其分配给gridview以外的gridview
现在我想更新数据正在更新,但是当我加载Gridview时,更新的行在编辑模式下显示但我不想在更新模式下
我尝试过:
I have even handlar as above which is empty
on row_Command I am getting data and assign it to textboxes presen outside of gridview
Now I want to update that data is is updating but when I Load Gridview whatever row updated is showing in edit mode but I dont want it in update mode
What I have tried:
<div class="row table-responsive grid" style="margin-top: 20px; margin: 1px">
<asp:GridView ID="gvMain" runat="server" HeaderStyle-BackColor="#336699" HeaderStyle-ForeColor="White" Font-Size="15px" PageSize="15"
AutoGenerateColumns="false" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Width="100%" OnRowDataBound="gvMain_RowDataBound"
OnRowCommand="gvMain_RowCommand" OnRowEditing="gvMain_RowEditing" OnRowDeleting="gvMain_RowDeleting">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Master_Ref_Value_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide" />
<asp:BoundField DataField="Main_Test_Id" HeaderStyle-CssClass="hide" ItemStyle-CssClass="hide" />
<asp:BoundField HeaderText="Sr.No">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle Height="30px" HorizontalAlign="Right"></HeaderStyle>
<ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
</asp:BoundField>
<asp:BoundField HeaderText="Ref Name" DataField="Ref_Name" HeaderStyle-HorizontalAlign="Center">
<ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
</asp:BoundField>
<asp:BoundField HeaderText="KeyVal" DataField="KeyVal" HeaderStyle-HorizontalAlign="Center">
<ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
</asp:BoundField>
<asp:BoundField HeaderText="Unit" DataField="Unit" HeaderStyle-HorizontalAlign="Center">
<ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
</asp:BoundField>
<asp:BoundField HeaderText="Value" DataField="Value" HeaderStyle-HorizontalAlign="Center">
<ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
</asp:BoundField>
<asp:BoundField HeaderText="Main_Test" DataField="Main_Test" HeaderStyle-HorizontalAlign="Center">
<ItemStyle Height="30px" HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="#CCCCCC" />
</asp:BoundField>
<asp:ButtonField ButtonType="Link" CausesValidation="false" HeaderText="Edit" ControlStyle-ForeColor="blue" CommandName="Edit" Text="Edit">
<HeaderStyle Width="30" />
</asp:ButtonField>
<asp:ButtonField ButtonType="Link" CausesValidation="false" HeaderText="Delete" ControlStyle-ForeColor="blue" CommandName="Delete" Text="Delete">
<HeaderStyle Width="30" />
</asp:ButtonField>
</Columns>
</asp:GridView>
</div>
btn在gridview之外更新
btn update outside of gridview
<div class="col-md-1">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btn btn-success" ValidationGroup="submit" OnClick="btnSubmit_Click" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" CssClass="btn btn-success" ValidationGroup="submit" OnClick="btnUpdate_Click" />
</div>
protected void gvMain_RowEditing(object sender, GridViewEditEventArgs e)
{
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
int MasterRefValueId = 0;
int MainTestId = 0;
int.TryParse(ddlMainTest.SelectedValue.ToString(), out MainTestId);
if (hdnMasterRefValueId.Value != null)
{
int.TryParse(hdnMasterRefValueId.Value.ToString(), out MasterRefValueId);
hdnMasterRefValueId.Value = null;
}
Master_ref_value objMaster_ref_value = new Master_ref_value(MasterRefValueId, MainTestId, txtRefName.Text, txtKeyVal.Text, txtUnit.Text, txtValue.Text);
bool result;
result = objMaster_ref_value.Update();
if (result)
{
lblMessage.Text = "Record Updated Successfully";
lblMessage.ForeColor = System.Drawing.Color.Green;
lblMessage.Visible = true;
Reset();
ClientScript.RegisterStartupScript(this.GetType(), "alert", "HideLabel();", true);
//gvMain.EditIndex = e.RowIndex - 1;
}
else
{
lblMessage.Text = "Record Updation failed";
lblMessage.ForeColor = System.Drawing.Color.Red;
lblMessage.Visible = true;
Reset();
ClientScript.RegisterStartupScript(this.GetType(), "alert", "HideLabel();", true);
}
Panel4.Visible = false;
fillgridview();
lblMessage.Visible = false;
EmpRegistrationpanel2.Visible = false;
EmpRegistrationpanel1.Visible = true;
}
我应该使用
where i should use
//gvMain.EditIndex = e.RowIndex - 1;
推荐答案
GridView
控件的默认行为是执行一次编辑和更新一行。您似乎正在尝试在GridView
之外使用更新按钮
,这样您无法识别哪一行你正在编辑。要更新一行,您应该处理RowUpdating
事件,然后在那里处理更新逻辑。
以下是几个例子:
使用TemplateFields
在GridView中添加具有编辑,更新和删除功能的行 [ ^ ]
使用BoundFields
GridView插入,编辑,更新和删除 - Ado.Net方式 [ ^ ]
现在,如果你想从中选择一行GridView
并填充GridView外部的TextBox
中的选定值,然后就可以摆脱RowEditing
事件和编辑ButtonField
。您只需要设置 ShowSelectButtonCommandField
在GridView
中,如:
The default behavior ofGridView
control is to perform edit and update one row at time. It seems like you are trying to use an UpdateButton
outside yourGridView
which there's no way for you to identify which row you are actually editing. To update a row, you should be handling theRowUpdating
event to do that and then handle your update logic there instead.
Here are a couple of examples:
Using TemplateFields
Adding Rows in GridView with Edit, Update and Delete Functionality[^]
Using BoundFields
GridView Insert, Edit, Update and Delete – The Ado.Net way[^]
Now, if you are trying to select a row fromGridView
and populate the selected values in yourTextBox
that is outside your GridView then you can get rid of theRowEditing
event and EditButtonField
. You would simply need to setup the ShowSelectButtonCommandField
in theGridView
like:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" >
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField />
<asp:BoundField />
</Columns>
</asp:GridView>
和 SelectedIndexChanged
事件你可以做这样的事情来得到选定的行数据并填充TextBox:
and at SelectedIndexChanged
event you can do something like this to get the selected row data and populate your TextBox:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e){
//Where index of Cells is the column in Grid that you wan't to refer
TextBox1.Text = GridView1.SelectedRow.Cells[0].Text;
TextBox2.Text = GridView1.SelectedRow.Cells[1].Text;
//If accessing TemplateField column then you can use FindControl method
}
那么你可以使用你在Update Button
中拥有的代码在 GridView
之外更新数据。
Then you can use the code that what you have in your Update Button
that is outside the GridView
to update the data.
这篇关于我可以在gridview之外制作gridview.editindex = e.rowindex - 1更新按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!