This is a migrated thread and some comments may be shown as answers.

Get item value

8 Answers 204 Views
Grid
This is a migrated thread and some comments may be shown as answers.
El
Top achievements
Rank 1
El asked on 01 Jan 2009, 10:28 PM
I have a custom grid column like following:

<telerik:GridTemplateColumn HeaderText="Image Url">
        <ItemTemplate>        
        <asp:Label ID="lblImageUrl" runat="server" Text='<%# Shorten(Eval("imageURL"), 20) %>'></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
        <asp:TextBox ID="txtImageUrl" runat="server" Text='<%# Eval("imageURL") %>'></asp:TextBox>
        </EditItemTemplate>
        </telerik:GridTemplateColumn>

what i need is to display an image when i either click select link or when i mouse over certain row. How do i get the lblImageUrl Text and from within which event handler?

Also i was wondering how do i update/edit record if i don't use SqlDataSource object .. actually i have a class with some methods to bind the grid :
Public Shared Function GetItems() As DataTable
            Dim con As SqlConnection = New SqlConnection("connString")
            Dim table As New DataTable
            Dim reader As SqlDataReader
            Dim cmd As New SqlCommand
            cmd.CommandText = "GetItems"
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Connection = con
            cmd.Parameters.Add("@param", SqlDbType.Int).Value = objectName
            ' execute the stored procedure and return the results
            Try
                cmd.Connection.Open()
                reader = cmd.ExecuteReader()
                table.Load(reader)
                reader.Close()
            Catch ex As Exception
                ' ToDo: catch the error if any
            Finally
                cmd.Connection.Close()
            End Try
            Return table
        End Function

then

Me.RadGridControl.DataSource = ClassName.GetItems
Me.RadGridControl.DataBind()

ANd it works fine. Means, so far so good. But now i am struggling with the update command.

Thank you



8 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 02 Jan 2009, 11:44 AM
Hello,

You can access the label text on clicking a button in any row of the grid as shown below:
aspx:
 <telerik:GridTemplateColumn UniqueName="TemplateColumn1"
            <ItemTemplate> 
                <asp:Button ID="Button" runat="server" Text="Select" OnClick="Button_Click" /> 
            </ItemTemplate> 
 </telerik:GridTemplateColumn> 
 <telerik:GridTemplateColumn UniqueName="TemplateColumn2"
             <ItemTemplate> 
                 <asp:Label ID="Label1" runat="server" Text='<%# Shorten(Eval("imageURL"), 20) %>'></asp:Label> 
             </ItemTemplate> 
 </telerik:GridTemplateColumn> 

cs:
 protected void Button_Click(object sender, EventArgs e) 
    { 
        Button btn = (Button)sender; 
        GridDataItem dataItem = (GridDataItem)btn.NamingContainer; 
        Label lbl = (Label)dataItem.FindControl("Label1"); 
        string strtxt = lbl.Text; 
    } 

And to perform Update/Insert/Delete operations on the grid when using StoredProcedures, you can use the same logic used to bind your grid and pass the input values as the parameters necessary to perform the various operations.

Thanks
Princy.
0
El
Top achievements
Rank 1
answered on 02 Jan 2009, 01:50 PM
Thank you Princy.
I was wondering if you can post very small sample how do i update the records using SP's?
I have created SP updateItem with four parameters where item_ID is the conditional param.
UPDATE table SET field1=@field1, field2=@field2 etc. where item_ID=@itemID
and then i have created boolean function and have added the needed parameters to the command object.
I just have no clue how to call this func and from where? UpdateCommand event handler maybe?
Thanks in advance
0
El
Top achievements
Rank 1
answered on 02 Jan 2009, 03:10 PM
Well, also the suggested approach for getting value of selected row is also not working.
I get the following error if i click the Button:

Sys.WebForms.PageRequestManagerServerErrorException: An unknown error occured while processing the request on the server. The status code returned from the server was: 500


Btw, with a common GridView control i am using the following code and it works from within RowCommand event handler.
<ItemTemplate> 
        <asp:ImageButton CommandName="preview" CommandArgument='<%# CType(Container, GridViewRow).RowIndex %>' ID="imgbtnPreview" runat="server" ImageUrl='<%# Eval("imageURL") %>' AlternateText='<%# Eval("alternateText") %>' Width="30px" /> 
</ItemTemplate> 


Protected Sub GridView1_RowCommand(ByVal sender As Object, _ 
               ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand 
     If e.CommandName = "preview" Then 
          Dim index As Integer = Convert.ToInt32(e.CommandArgument) 
          Dim row As GridViewRow = gridname.Rows(index) 
          Response.Write(index.ToString()) 
          ' etc 
     End If 
End Sub 

Thanks in advance for any further help

0
El
Top achievements
Rank 1
answered on 03 Jan 2009, 07:44 AM
I am getting the following error:

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.


If i set the EnableEventValidation to False i don't get this error but then the event is raised only on every second click

Dim btn As ImageButton = CType(sender, ImageButton)
Dim dataItem As GridDataItem = CType(btn.NamingContainer, GridDataItem)
Dim lbl As Label = CType(dataItem.FindControl("lblImageUrl"), Label)
Me.RadGridControl.MasterTableView.Columns(2).HeaderText = lbl.Text

Means i get the expected result but only after i click the certain image twice.

Please advice me how do i overcome this odd situation.
0
El
Top achievements
Rank 1
answered on 03 Jan 2009, 09:34 AM
Is the support this slow always, or this is due to new year and Christmas?

Telerik controls were given to me by the client that i work for but, i am not happy at all with the support and work with the control.
RAD should stand for rapid development per my understanding but, this is not the case. The controls are over-complicated and very confusing. I think i will try to convince the client to back over common GridView control.
0
El
Top achievements
Rank 1
answered on 06 Jan 2009, 04:01 PM
No answer? BUMP
0
Accepted
Sebastian
Telerik team
answered on 06 Jan 2009, 04:21 PM
Hello El,

The delay in our reply is due to the fact that we had limited number of support resources during the Christmas and New Year holidays - please excuse us for that.

As to the issue in question:
Can you please verify that you are using advanced binding intercepting the NeedDataSource event of the grid? Note that operations like data editing, grouping, etc. require such type of binding as opposed to the standard simple GridView binding with DataBind() calls.

Additionally, you can review the online demos under the Insert/Update/Delete section of the QSF or the online documentation, or search for a particular piece of information using the new search facilities on telerik.com.

Kind regards,
Sebastian
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
El
Top achievements
Rank 1
answered on 07 Jan 2009, 10:12 PM
Ok i managed to edit the GridView but i kept using the old approach which is calling the DataBind :)

Thanks for the links anyway.
Tags
Grid
Asked by
El
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
El
Top achievements
Rank 1
Sebastian
Telerik team
Share this question
or