使用行命令在网格视图链接按钮上单击填充表单 [英] Populate the form on grid view link button click using row command
本文介绍了使用行命令在网格视图链接按钮上单击填充表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我每个人在我的表单中都有一个gridview,因为我有编辑和删除链接按钮,我想根据我在gridview中选择的行填写表单文本框字段值。这是我的页面的标记
Hi Every one I have a gridview in my form , in that I have Edit and Delete link button , I want to fill the form text boxes field value based on the Row I have selected in the gridview. Here is the Markup of my page
<html>
<table>
<tr>
<td>
<table id="Table4" border="0" cellpadding="1" cellspacing="1" class="normal"
width="600">
<tr>
<td align="center" class="style5" colspan="3">
</td>
</tr>
<tr style="color: #cc0000">
<td class="HeaderText" colspan="3">
<asp:Label ID="Label1" runat="server" Text="Add New Notice "></asp:Label>
<hr />
</td>
</tr>
<tr>
<td class="style1">
</td>
<td>
</td>
<td class="normal">
</td>
</tr>
<TR>
<TD class="style1">Date of Notice :<font color="#ff0033">*</font></TD>
<TD class="normal"> </TD>
<TD class="normal"> </TD>
</TR>
<TR>
<TD class="style1">
<asp:TextBox ID="txtnoticedate" runat="server" Width="200px" CssClass="textbox"></asp:TextBox>
<ajax:CalendarExtender ID="txtnoticedate_CalendarExtender" runat="server"
Enabled="True" TargetControlID="txtnoticedate" PopupButtonID="imgbtncalendar">
</ajax:CalendarExtender>
<asp:ImageButton ID="imgbtncalendar" runat="server"
ImageUrl="~/Admin/Images/Calendar.png" />
</TD>
<TD class="style6">
</TD>
<TD class="style6">
</TD>
</TR>
<TR>
<TD class="style1">Title :<font color="#ff0033">*</font></TD>
<TD class="normal"> </TD>
<TD class="normal"> </TD>
</TR>
<TR>
<TD class="style1">
<asp:TextBox
ID="txtnoticetitle" runat="server" Width="200px" CssClass="textbox"></asp:TextBox>
</TD>
<TD>
</TD>
<TD>
</TD>
</TR>
<TR>
<TD class="style1">
Notice Body :<font color="#ff0033">*</font></TD>
<TD>
</TD>
<TD>
</TD>
</TR>
<TR>
<TD class="style1">
<asp:TextBox
ID="txtnoticebody" runat="server" Width="200px" CssClass="textbox"
TextMode="MultiLine"></asp:TextBox>
</TD>
<TD>
</TD>
<TD>
</TD>
</TR>
<TR>
<TD class="style1">
<asp:Button ID="btnSubmit" runat="server"
Text="Submit" CssClass="GridHeader" onclick="btnSubmit_Click" />
</TD>
<TD style="HEIGHT: 18px; text-align: left;" align="center">
</TD>
<TD style="HEIGHT: 18px">
</TD>
</TR>
<TR id="imageDot" runat="server">
<TD align="center" colSpan="3" style="height: 14px">
<asp:Label ID="lblerrormessage" runat="server" CssClass="errormsg" ForeColor="Red"></asp:Label>
</TD>
</TR>
<TR>
<TD align="center" colSpan="3">
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" ShowSummary="False" />
</TD>
</TR>
<TR>
<TD align="center" colSpan="3">
<asp:GridView ID="GridView1" runat="server" CellPadding="3" CssClass="GridItem"
HorizontalAlign="Center"
onselectedindexchanged="GridView1_SelectedIndexChanged"
onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnedit" runat="server" CausesValidation="False"
CommandName="RowUpdate" ForeColor="Black">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="lnkbtndelete" runat="server" CausesValidation="False"
CommandName="RowDelete" ForeColor="Black">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BorderStyle="Solid" BorderWidth="1px" CssClass="GridHeader" HorizontalAlign="Left" />
</asp:GridView>
</TD>
</TR>
<TR>
<TD align="center" colSpan="3">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="btnSubmit" />
</Triggers>
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</TD>
</TR>
</table>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
Here is the code
Here is the code
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("RowUpdate"))
{
txtnoticedate.Text = GridView1.SelectedRow.Cells[1].Text;
txtnoticetitle.Text = GridView1.SelectedRow.Cells[2].Text;
txtnoticebody.Text = GridView1.SelectedRow.Cells[3].Text;
}
}
It show Null reference exception . Please Help . Thanks in advance
It show Null reference exception . Please Help . Thanks in advance
推荐答案
Pass ID by CommandArgument='<%# Eval("Id") %>'
Then Get that id here by Session
if (e.CommandName == "RowUpdate")
{
Session["Id"] = e.CommandArgument.ToString();
}
I added a command argument to the link button
I added a command argument to the link button
<itemtemplate>
<asp:linkbutton id="lnkbtnedit" runat="server" causesvalidation="False" xmlns:asp="#unknown">
CommandName="RowUpdate" ForeColor="Black" CommandArgument='<%#((GridViewRow)Container).RowIndex%>'>Edit</asp:linkbutton>
</itemtemplate>
then in the code i fetched the row index
then in the code i fetched the row index
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("RowUpdate"))
{
int index = Convert.ToInt32(e.CommandArgument);
txtnoticedate.Text = GridView1.Rows[index].Cells[2].Text;
}
}
这篇关于使用行命令在网格视图链接按钮上单击填充表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文