开发者

Edit row in gridview with hyperlink in ASP.NET

开发者 https://www.devze.com 2023-02-24 17:24 出处:网络
I\'ve got a gridview displaying product instance info; I need a hyperlink in my Action column to bring up an view/edit page that开发者_StackOverflow社区 displays the row data. How do I make the link b

I've got a gridview displaying product instance info; I need a hyperlink in my Action column to bring up an view/edit page that开发者_StackOverflow社区 displays the row data. How do I make the link bring up the data from that specific row into the edit page?

Note: there are other questions with similar titles, however, they do not cover this specific topic.


Use datakeys in the gridview, using datakey will get you the id of each clicked hyperlink , and then you can use that id to edit or delete the selected items easily. In the code behind just find the hyperlink control , pass the data key and write d update sql for it. Inorder to move your data to other pages you can sessions but if you are developing a commercial website session wont be a good idea due to its security issues, use cookies in that case .

 protected void Page_Load(object sender, EventArgs e)
{   
    if (!Page.IsPostBack)
    {
        if (Request.QueryString["productID"] != null)
        {
            productID = Convert.ToInt32(Request.QueryString["productID"]);
            bindData(productID)
        }
        ...
    }
 }
   protected void bindData(int productID)
    {
     //to avoid sql injection as mentioned below use parameters 
      SqlConnection conn = new SqlConnection(ConnectionString); // define connection string globally or in your business logic
      conn.Open();
      SqlCommand sql = new SqlCommand("Select * From [Table] Where ID = @productID",conn);
      SqlParameter parameter = new SqlParameter();
      parameter.ParameterName = "@ID";
   parameter.Value = productID;
       sql.Parameters.Add(parameter);
       conn.close()
  }

You can also use Microsoft.ApplicationBlocks.Data.dll to avoid repeating ado.net , it will reduce your code.


Try something like this?

ViewProducts.aspx:

<columns>
    <asp:HyperLinkField DataNavigateUrlFields="ProductID" HeaderText="Edit" 
        ItemStyle-Width="80" 
            DataNavigateUrlFormatString="EditProduct.aspx?productID={0}" 
                Text="Select" ItemStyle-HorizontalAlign="Center" />
    ...
</columns>

EditProduct.aspx:

    protected void Page_Load(object sender, EventArgs e)
    {   
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["productID"] != null)
            {
                productID = Convert.ToInt32(Request.QueryString["productID"]);
                ...
            }
            ...
        }
     }


There are n+1 ways to solve this problem. If you are using the sql data source you can accentually have VS generate the sql and edit logic for you if you don't have specific requirements. here is a code project tutorial.

Another oft used tactic is to add a command button to the row and populate the command argument with the ID of the row you want to edit then in the oncommand event handle what ever logic you need.

you can also use a simple html link and and use get parameters. Or you can session like I said there a a ton of ways to solve this problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号