LinkButton in a datagrid???

Discussion in 'ASP .Net' started by =?Utf-8?B?VGltOjouLg==?=, Jan 25, 2005.

  1. 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
     
    =?Utf-8?B?VGltOjouLg==?=, Jan 25, 2005
    #1
    1. Advertising

  2. 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>
    "Tim::.." <myatix_at_hotmail.com> wrote in message
    news:...
    > 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
     
    Ken Cox [Microsoft MVP], Jan 25, 2005
    #2
    1. Advertising

  3. 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
     
    =?Utf-8?B?VGltOjouLg==?=, Jan 25, 2005
    #3
  4. 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>

    "Tim::.." <myatix_at_hotmail.com> wrote in message
    news:...
    > 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
    >
    >
     
    Ken Cox [Microsoft MVP], Jan 25, 2005
    #4
  5. 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


    "Ken Cox [Microsoft MVP]" wrote:

    > 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>
    >
    > "Tim::.." <myatix_at_hotmail.com> wrote in message
    > news:...
    > > 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
    > >
    > >

    >
    >
     
    =?Utf-8?B?VGltOjouLg==?=, Jan 26, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vik
    Replies:
    0
    Views:
    352
  2. Mark
    Replies:
    1
    Views:
    492
  3. Dude
    Replies:
    0
    Views:
    370
  4. Sathyaish
    Replies:
    3
    Views:
    3,169
    Sathyaish
    Sep 8, 2005
  5. Sathyaish

    Linkbutton does not look like a linkbutton

    Sathyaish, Sep 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    217
    Sathyaish
    Sep 8, 2005
Loading...

Share This Page