LinkButton in a datagrid???

G

Guest

Can someone tell me how I do a response.redirect using a linkbutton in a
datagrid!

I want to use the record id in a database to send a querystring to an HTML
editor so that users are able to edit a particular page they have chosen from
a datagrid!

EG: If a user clicks the linkbutton in a datagrid it takes you to a page
with the following url...

.../editor.aspx?id=23

Would appritiate any help!

Thanks
 
K

Ken Cox [Microsoft MVP]

Hi Tim,

Here's the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto


Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
DataGrid1.DataSource = CreateDataSource()
DataGrid1.DataBind()
End If
End Sub

Private Sub DataGrid1_ItemCommand _
(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.ItemCommand
' Check for the right link button
If e.CommandName = "Select" Then
' Build the redirection URL
Response.Redirect("othersite.aspx?id=" & e.CommandArgument)
End If
End Sub

Function CreateDataSource() As DataTable
'ProductId
'DownloadURL()
'title()
Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add(New DataColumn _
("ProductId", GetType(Int32)))
dt.Columns.Add(New DataColumn _
("DownloadURL", GetType(String)))
dt.Columns.Add(New DataColumn _
("title", GetType(String)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "URL " + i.ToString()
dr(2) = "Item " + i.ToString()
dt.Rows.Add(dr)
Next i
Return dt
End Function 'CreateDataSource

<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" runat="server">
<Columns>
<asp:BoundColumn DataField="ProductId"
Visible="False"></asp:BoundColumn>
<asp:BoundColumn DataField="DownloadURL"
Visible="False"></asp:BoundColumn>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="lnkArticle"
CommandArgument='<%#DataBinder.Eval(Container, "DataItem.ProductId")%>'
Runat="server" Text='<%#DataBinder.Eval(Container, "DataItem.Title")%>'
CommandName="Select" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
 
G

Guest

Hi Ken,

Thanks for the reply! I have managed to get the copy you sent me to work but
for some reason I just can't get to get it to work with my code!

I am using a datagrid rather than a datatable! Would this effect it in a big
way!

I would be really interested why this doesn't work!

Thanks


This is my code..

<asp:datagrid id="DGPages" runat="server" EnableViewState="False"
AlternatingItemStyle-BackColor="WhiteSmoke" CssClass="txtArea"
ShowFooter="false" HeaderStyle-Font-Bold="True"
HeaderStyle-HorizontalAlign="Center" CellPadding="4" Width="50%"
AutoGenerateColumns="False"
BorderStyle="Ridge" BorderWidth="1px" GridLines="Horizontal"
BorderColor="LightGray">
<EditItemStyle BackColor="#EEEEEE"></EditItemStyle>
<AlternatingItemStyle BackColor="WhiteSmoke"></AlternatingItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center"
ForeColor="White" BackColor="Black"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="PageId"
Visible="False"></asp:BoundColumn>

<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<img src="../images/page.gif">
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Page">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("header") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Description">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<%# Container.DataItem("description") %>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:LinkButton ID="PageEdit"
CommandArgument='<%#DataBinder.Eval(Container, "DataItem.PageId")%>'
Text='<img border=0 src=../images/edit.gif alt=Edit Page>'
CommandName="Select" Runat="server">
</asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:LinkButton ID="PageProperties"
CommandArgument='<%#DataBinder.Eval(Container, "DataItem.PageId")%>'
Runat="server" Text="<img border=0 src=../images/properties.gif alt=Page
Properties>" CommandName="Properties">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>





Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

If Not Page.IsPostBack Then

BindData()



End If

End Sub

Sub BindData()

Dim Myconn As New SqlConnection(ConfigurationSettings.AppSettings("strConn"))

Dim cmd As New SqlCommand("PageDetails", Myconn)

cmd.CommandType = CommandType.StoredProcedure

Myconn.Open()

Dim objOfficeName, objID As SqlParameter

objOfficeName = cmd.Parameters.Add("@OfficeName", SqlDbType.Char)

'objID = cmd.Parameters.Add("@ID", SqlDbType.Int)

objOfficeName.Direction = ParameterDirection.Input

'objID.Direction = ParameterDirection.Input

objOfficeName.Value = FindOffice()

'objID.Value = 2

Dim myReader As SqlDataReader = cmd.ExecuteReader()

Dim hasRows As Boolean

hasRows = True

DGPages.DataSource = myReader

DGPages.DataBind()

If DGPages.Items.Count = 0 Then

lblNoContent.Visible = True

lblNoContent.Text = "There a currently no pages for" & " " & FindOffice()

End If

myReader.Close()

Myconn.Close()

End Sub 'Loads Data into Datagrid



Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DGPages.ItemCommand

' Check for the right link button

If e.CommandName = "Select" Then

' Build the redirection URL

Response.Redirect("../default.aspx?id=" & e.CommandArgument)

End If

End Sub
 
K

Ken Cox [Microsoft MVP]

Hey Tim,

Why not do it the simple way and use a hyperlink column?

<asp:hyperlinkcolumn DataNavigateUrlField="pageid"
DataNavigateUrlFormatString="destination.aspx?id={0}"
DataTextField="PageId"></asp:hyperlinkcolumn>
 
G

Guest

This doesn't provided me with what I require as far as I know!

I also get an error saying "The active scheme does not support the element!

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top