dimanche 28 juin 2015

how to get data using OnRowCommand event in c# ASP.NET

I can not fetch data while using OnRowCommand event of GridView using c# ASP.NET.i am using linkButton inside gridview.I am explaining my code below.

faq.aspx.cs:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            //int faqID = int.Parse(e.CommandArgument.ToString());
            switch (e.CommandName)
            {
                case "DoEdit":
                    {
                        int index = Convert.ToInt32(e.CommandArgument);
                        GridViewRow row = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                        int RowIndex = row.RowIndex;
                        TextBox1.Text = GridView1.Rows[RowIndex].Cells[1].Text;
                        TextBox2.Text = GridView1.Rows[RowIndex].Cells[2].Text;
                        HiddenField1.Value = GridView1.Rows[RowIndex].Cells[0].Text;
                        Button1.Text = "Update";
                        break;
                    }
                case  "DoDelete":
                   {
                       faqBL objFaqBl = new faqBL();
                       int index = Convert.ToInt32(e.CommandArgument);
                       int faqID =Convert.ToInt16( GridView1.Rows[index].Cells[0].Text);
                       objFaqBl.deleteFaqData(faqID);
                       break;
                   }
            }
        }

I am passing OnRowCommand from GridView like below.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
                                            Width="100%" CssClass="table table-striped table-bordered margin-top-zero" 
                                            OnRowCommand="GridView1_RowCommand"  >
                                        <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></a> -->
                                            <asp:LinkButton ID="HyperLink1" runat="server" data-toggle="tooltip" title="" class="btn btn-xs btn-success" data-original-title="Edit" CommandName="DoEdit" CommandArgument='<%# Eval("FAQ_ID") %>' ><i class="fa fa-edit"></i></asp:LinkButton>
                                             <asp:LinkButton ID="HyperLink2" runat="server" data-toggle="tooltip" title="" class="btn btn-xs btn-danger" data-original-title="Delete" CommandName="DoDelete" CommandArgument='<%# Eval("FAQ_ID") %>' ><i class="fa fa-times"></i></asp:LinkButton>
                                           </ItemTemplate>
                                           </asp:TemplateField>
                                        </Columns>
                                        </asp:GridView>

Please help me to resolve this issue.

Aucun commentaire:

Enregistrer un commentaire