[英]Edit Image and Delete Row from GridView using c# ASP.NET
I need to Edit
and Delete
Row
of GridView
using C# ASP.NET
. 我需要使用
C# ASP.NET
Edit
和Delete
GridView
Row
。
I tried once and able to fill the data in TextBox
after click
on Edit Button
,But I have also one Image
to Edit
and what I need is when user will click
on Edit Image
, The Image
will also Display
in proper place to Edit
.In case of Delete
part I have Image
in Anchor Tag
and I need which event I should pass from GridView
and define in code behind page so that I can do the operation. 我尝试过一次,并且在
click
“ Edit Button
后能够在TextBox
填充数据,但是我还有一个要Edit
Image
,我需要的是当用户click
“ Edit Image
, Image
也将Display
在适当的位置以进行Edit
。在Delete
部件的情况下, Anchor Tag
有Image
,我需要从GridView
传递哪个事件并在页面后面的代码中定义,以便执行此操作。
faq.aspx:
faq.aspx:
<div class="col-md-6">
<label for="question" accesskey="T"><span class="required">*</span> Question</label>
<asp:TextBox ID="TextBox1" runat="server" size="30" value="" name="question" ></asp:TextBox>
<div id="noty" style="display:none;" runat="server"></div>
<label for="answer" accesskey="A"><span class="required">*</span> Answer</label>
<asp:TextBox ID="TextBox2" runat="server" size="30" value="" name="answer" ></asp:TextBox>
<div id="Div1" style="display:none;" runat="server"></div>
</div>
<div class="col-md-6 bannerimagefile">
<label for="insertimage" accesskey="B"><span class="required">*</span> Insert Image</label>
<asp:FileUpload runat="server" class="filestyle" data-size="lg" name="insertimage" id="FileUpload1" onchange="previewFile()" />
<label for="bannerimage" accesskey="V"><span class="required">*</span> View Image</label>
<div style="padding-bottom:10px;">
<asp:Image ID="Image3" runat="server" border="0" name="bannerimage" style="width:70px; height:70px;" />
</div>
<div class="clear"></div>
<asp:Button ID="Button1" runat="server" Text="Submit" class="submit"
onclick="Button1_Click" />
</div>
</div>
</div>
</div>
<!--end_1st_faq_add_div-->
<!--2nd_list_banner_view_div-->
<div class="widget-area">
<h2 class="widget-title"><strong>FAQ List</strong></h2><asp:HiddenField ID="HiddenField1" runat="server" />
<div class="streaming-table margin-top-zero padding-top-zero">
<div class="table-responsive">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
Width="100%" CssClass="table table-striped table-bordered margin-top-zero"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField HeaderText="Sl No">
<ItemTemplate>
<asp:Label ID="faqid" runat="server" Text='<%#Eval("FAQ_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Question" >
<ItemTemplate>
<asp:Label ID="question" runat="server" Text='<%#Eval("Question") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Answer" >
<ItemTemplate>
<asp:Label ID="answer" runat="server" Text='<%#Eval("Answer") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image" >
<ItemTemplate>
<asp:Image ID="Image1" runat="server" border="0" name="bannerimage" style="width:70px; height:70px;" ImageUrl='<%# "/Upload/" + Convert.ToString(Eval("Image")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action" >
<ItemTemplate>
<a href="" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="Edit" id="editbtn" ><i class="fa fa-edit"></i></a>
<a href=" " data-toggle="tooltip" title="" class="btn btn-xs btn-danger" data-original-title="Delete"><i class="fa fa-times"></i>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
faq.aspx.cs:
faq.aspx.cs:
protected void GridView1_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
{
int index = Convert.ToInt32(e.NewSelectedIndex);
TextBox1.Text = GridView1.Rows[index].Cells[1].Text;
TextBox2.Text = GridView1.Rows[index].Cells[2].Text;
HiddenField1.Value = GridView1.Rows[index].Cells[0].Text;
Button1.Text = "Update";
}
Please help me to resolve this issue. 请帮助我解决此问题。
I see that you have only item temples in grid view. 我看到您只有网格视图中的项目边撑。 Hence I'm going to tell you two ways to do this: 1) Add edit template in grid view and handle the OnRowEditing event of grid view.
因此,我将告诉您两种方法:1)在网格视图中添加编辑模板,并处理网格视图的OnRowEditing事件。 2) Add a hyperlink with key of row and link to another page where you can design the editor like you have done in this page by pre-populating the data using the key(primary key)
2)添加带有行键的超链接,然后链接到另一个页面,您可以像在此页面中一样设计编辑器,方法是使用键(主键)预先填充数据
it is better that you use Modern data control like 'entitydataSource' or 'linqdateSource' or 'sqlDataSource' and bind your gridview by them . 最好使用“ entitydataSource”或“ linqdateSource”或“ sqlDataSource”之类的现代数据控件,并通过它们绑定您的gridview。 using 'itemtemplate' for all rows is not a good way , instead fill you grid with 'dataSource' and use 'itemTemplate' for delete or Edit Button .
对所有行使用'itemtemplate'并不是一个好方法,而是将'dataSource'填充到网格中,并使用'itemTemplate'进行删除或编辑按钮。 send button name As commandName and rowID As commandArgument to gridViewItemCommand event in code behind.
在后面的代码中将按钮名称作为commandName和rowID作为commandArgument发送到gridViewItemCommand事件。
in code behindin in GridviewItemcommand_Event with switch statement loop through itemCommands like this : 在GridviewItemcommand_Event中的落后代码中,带有switch语句的itemCommands循环如下:
int itemID = int.parse(e.commandArgument)
switch(e.commandName)
{
case 'DoEdite' :{//some Code
Viewstate["ID"] = itemID;
break;}
case 'DoDelete' :{//some Code
break;}
}
you have the itemID(e.commandeArgument) and know which button clicked(e.commandName) .so you can do what you want. 您具有itemID(e.commandeArgument)并知道单击了哪个按钮(e.commandName)。因此您可以执行所需的操作。 In edit Mode when you send data to textBoxes use viewstate or other collection to hold your dataID because of after Edit, for Update edited date you need it,
在编辑模式下,当您将数据发送到文本框时,由于在编辑后需要使用viewstate或其他集合来保存您的dataID,因此对于更新编辑日期,您需要它,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.