本文介绍了Gridview Row命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



案例



我有网页表格,显示来自数据库的一些记录gridview。

1.我使用文本框和按钮控件提供了搜索工具。当用户在文本框中输入内容并单击按钮时,gridview会更新搜索结果。

2.我的gridview位于更新面板内。

3.我已经编辑了使用gridview链接按钮删除记录。当用户点击编辑链接时

页面重定向到使用查询字符串编辑记录ID的表单。

4. Gridview分页已启用。

5.我用数据集来填充gridview。



显示所有记录时(搜索,编辑,删除)工作正常精细。但是当用户搜索某些记录并单击编辑链接时,rowcommand获取gridview的命令参数,其中包含所有记录,即搜索之前的状态,最后它会重定向到编辑错误ID的表单。



代码:

搜索按钮点击事件:



Hello there,

Case

I have web form that shows some record from database using a gridview.
1. I have given search facility in form using a textbox and button control. When user enter something in textbox and click on button the gridview updates with search results.
2. My gridview is inside a update panel.
3. I have given edit and delete record in using gridview link buttons.When user click on edit link
the page redirect to edit form with record id using query string.
4. The Gridview paging is enabled.
5. I have used dataset to fill the gridview.

While showing all records everything (Search, Edit, Delete) is working fine. But when user search for some record and click on edit link the rowcommand gets command argument of gridview having all record i.e previous state before search and final it redirects to edit form with wrong id.

Code:
Search Button Click Event:

protected void Button1_Click(object sender, EventArgs e)
                {

                     refreshbothdd();
                     string SearchData;
                     int SearchIn;
                     SearchData = TextBox1.Text;
                     SearchIn = DropDownList3.SelectedIndex;
                     switch(SearchIn)
                     {
                     case 1:

                     FillGrid(4,SearchData );
                     break;
                     case 2 :
                     FillGrid(5,SearchData );
                     break;
                 }





根据不同查询填写网格的方法:





Method to fill the grid according to Different Queries:

public void FillGrid(int temp, string tempData)
             {
                     switch (temp)
                     {
                     case 0:
                     da = new SqlDataAdapter("Select SRNo, DepositeType, PartyName, AccOf, CreditOf, DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable order by SRNo desc ", conn);
                     break;
                     case 1:
                     da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where DepositeBy='" + tempData + "' order by SRNo desc", conn);
                     break;
                     case 2:
                     da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where AccOf='" + tempData + "' order by SRNo desc", conn);

                     break;
                     case 3:
                     da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where DepositeType='" + tempData + "' order by SRNo desc", conn);
                     break;
                     case 4:
                     da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where ChequeNo like '%" + tempData + "%' order by SRNo desc", conn);
                     break;
                     case 5:
                     da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where PartyName like '%" + tempData + "%' order by SRNo desc", conn);
                     break;
                     case 6:
                     da = new SqlDataAdapter("Select SRNo, DepositeType,PartyName,AccOf,CreditOf,DepositeDt,BankName,BranchName,ChequeNo,Amount,DepositeBy from ChTable where DepositeDt between '" + fromdt.Text + "' and '" + todt.Text + "' order by SRNo desc", conn);
                     break;
                     }

                     GridView1.DataSource = null;
                     GridView1.DataBind();

                     ds = new DataSet();
                     da.Fill(ds);
                     GridView1.DataSource = ds.Tables[0];
                     GridView1.DataBind();

             }





GridView Rowcommand For Edit:





The GridView Rowcommand For

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
                 {

                         if (e.CommandName == "Edit")
                         {
                         string SRID = e.CommandArgument.ToString();
                         Response.Redirect("EditTransact.aspx/?Transaction=" + SRID + "");
                         }

                 }







代码发布在这里添加注释以便更好地理解[/ Edit]




Code posted in the comment is added here for better understanding[/Edit]

推荐答案

<asp:scriptmanager id="ScriptManager1" runat="server" enablepartialrendering="false" xmlns:asp="#unknown"></asp:scriptmanager>


这篇关于Gridview Row命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:09