Can PostbackUrl be set for GridView CommandField?

I have a GridView control on my page for which I have defined a BoundFields for series. Each row of the GridView database has a CommandField (Select) for which I want to send a PostBack to a new page.

Of course, I could easily send the NewSelectedIndex to a QueryString, but I would rather keep this information hidden from the user. Suggestions?

0


source to share


3 answers


Leppi is right. The GridView has no PostbackUrl property. However, you can do what you want using a standard control that has a PostbackUrl property.

<asp:TemplateField AccessibleHeaderText="Edit">
    <ItemTemplate>
        <asp:Button runat="server" ID="btnEdit" PostBackUrl="~/Default.aspx" OnClientClick='form1.ActivityId.value = this.Tag;' Tag='<%# Eval("ActivityId") %>' Text="Edit"/>
    </ItemTemplate>
</asp:TemplateField>

      



In this code example, I've added a template to the columns of the GridView. I am using a dynamically added tag attribute for a button to pass the id, and then I use Javascript code to put the value in the hidden field, and then the button just returns to the page specified in the PostbackUrl property.

+1


source


Use HyperLinkField column in GridView:

<asp:HyperLinkField AccessibleHeaderText="Edit" DataNavigateUrlFields="ActivityId" DataNavigateUrlFormatString="AcitivtyEdit.aspx?id={0}" Text="Edit Activity" />

      



Of course, as you said, this parameter shows the id in the url. To hide this (although anyone can check your javascript and see what you are doing) you have two options: 1. Use Javascript to set the hidden id field, change the form (action) feedback address, and then submit the form ... You have to allow the asp.net site to allow postbacks from another page 2. Allow the command to return the page to the same page that hosts the GridView and server side event handler to the server. Submitting to your page (this option presents additional problems with sequential postbacks ...)

0


source


Thanks to Sergiu Damian.

I am using MasterPages and the hidden UniqueID field may be different. I need to post two values.
In case Gridview_RowDataBound I put this code:

If e.Row.RowType = DataControlRowType.DataRow Then
   e.Row.Cells(12).Text = ""
   Dim img As New WebControls.ImageButton
   img.PostBackUrl = "NecesidadesPostBack.aspx"
   img.ImageUrl = "imagenes/edit.png"
   img.OnClientClick = HDLB.UniqueID.ToString & ".value = '" & e.Row.Cells(0).Text & "'; " & HDPT.UniqueID.ToString & ".value = '" & e.Row.Cells(1).Text & "';"
   img.AlternateText = "Edit"
   e.Row.Cells(12).Controls.Add(img)
End If

      

I have 2 hidden fields (HDLB and HDPT). The OnClientClick event changes the value of these hidden fields with the value of the first and second cells on the grid.

0


source







All Articles