Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
<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


C#
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
Posted
Comments
jackspero18 30-Jul-13 3:56am    
Pass ID by CommandArgument='<%# Eval("Id") %>'

Then Get that id here by Session

if (e.CommandName == "RowUpdate")
{

Session["Id"] = e.CommandArgument.ToString();

}

XML
Pass ID by CommandArgument='<%# Eval("Id") %>'

Then Get that id here by Session

if (e.CommandName == "RowUpdate")
        {

            Session["Id"] = e.CommandArgument.ToString();

        }
 
Share this answer
 
I added a command argument to the link button
ASP.NET
<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

C#
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;
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900