简体   繁体   English

如何将gridview中选定行的数据传输到另一个aspx页面到asp控件中?

[英]How to transfer data of selected row in gridview to another aspx page into asp controls?

i don't understand where to write code for this operation.我不明白在哪里为这个操作编写代码。

I tried to implement code from following link https://forums.asp.net/t/2130391.aspx?RowEditing+in+GridView+in+c+ of Cathy Zou.我尝试通过以下链接实现代码https://forums.asp.net/t/2130391.aspx?RowEditing+in+GridView+in+c+ of Cathy Zou。 But didn't understand code behind.但是没看懂后面的代码。

This is my gridview code.这是我的 gridview 代码。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1"  >
     <Columns>                            
         <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
         <asp:BoundField DataField="pass" HeaderText="pass" SortExpression="pass" />
         <asp:CommandField ShowDeleteButton="false" ShowEditButton="True" ButtonType="Image" HeaderText="Edit" EditImageUrl="~/images/editicon.jpg"/>
         <asp:CommandField ShowDeleteButton="True" ShowEditButton="false"  ButtonType="Image" HeaderText="delete" DeleteImageUrl="~/images/deleteicon.png"/>
     </Columns>
     <HeaderStyle CssClass="gridviewheader" />
     <RowStyle CssClass="gridviewrow" />
</asp:GridView>

This is my codebehind code.这是我的代码隐藏代码。

public string cnstring = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True";

protected void Page_Load(object sender, EventArgs e)
{
   if(!this.IsPostBack)
   {
       binddata();
   }
}

protected void binddata()
{

}

I have two pages, called addproduct.aspx and editproduct.aspx.我有两个页面,分别称为 addproduct.aspx 和 editproduct.aspx。 In addproduct.aspx page i have gridview along with Edit and delete commandfield.在 addproduct.aspx 页面中,我有 gridview 以及编辑和删除命令字段。 When i click on edit it should redirect me to editproduct.aspx page with selected row data.当我单击编辑时,它应该将我重定向到带有选定行数据的 editproduct.aspx 页面。

My requirement is, i want to display edit icon for edit instead edit link.我的要求是,我想显示用于编辑的编辑图标而不是编辑链接。 When i click on edit icon it should work according to my requirements.当我点击编辑图标时,它应该根据我的要求工作。

I would suggest adding a TemplateField instead of CommandField, so you can add an asp:Button like this:我建议添加一个 TemplateField 而不是 CommandField,这样你就可以像这样添加一个 asp:Button:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" />
        <asp:BoundField DataField="pass" HeaderText="pass" SortExpression="pass" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server"  ID="deleteButton" OnClick="deleteButton_Click" Text="Delete"/>                    
            </ItemTemplate>
            <ItemTemplate>
                <asp:Button runat="server" ID="editButton" OnClick="editButton_Click" Text="Edit"/>
            </ItemTemplate>
        </asp:TemplateField>            
    </Columns>
    <HeaderStyle CssClass="gridviewheader" />
    <RowStyle CssClass="gridviewrow" />
</asp:GridView>

And in the addproduct.aspx , inside the OnClick event, capture the row data and send it to editproduct.aspx like this:addproduct.aspx ,在 OnClick 事件中,捕获行数据并将其发送到editproduct.aspx如下所示:

string id = GridView1.SelectedRow.Cells[0].Text;
//sending the datafield "id" to the other webform
Response.Redirect("editproduct.aspx?idParameter=id"); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM