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

How to set controls' property on item command?

4 Answers 219 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Singl Pang
Top achievements
Rank 1
Singl Pang asked on 17 Oct 2012, 01:33 AM
Hi All:
    As below, I want to set the label no-show when I click the "Delete" Button. But it is not fire.
    <telerik:RadGrid ID="RadGrid1" runat="server"
        AutoGenerateColumns="False" CellSpacing="0" AllowPaging="True" GridLines="None" Skin="Default">
  
<MasterTableView>
 
 
 
    <Columns>
        <telerik:GridTemplateColumn DataField="AREA_ZIP_CODE" UniqueName="AREA_ZIP_CODE">
            <HeaderTemplate>
                <asp:Label ID="Label2" runat="server" Text="AREA_ZIP_CODE"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("AREA_ZIP_CODE") %>' Visible=false></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridTemplateColumn DataField="AREA_ZIP_DESC" UniqueName="AREA_ZIP_DESC">
            <HeaderTemplate>
                <asp:Label ID="Label3" runat="server" Text="AREA_ZIP_DESC"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("AREA_ZIP_DESC") %>' Visible=false></asp:Label>
            </ItemTemplate>
        </telerik:GridTemplateColumn>
        <telerik:GridBoundColumn DataField="ACTIVE_FLAG" HeaderText="ACTIVE_FLAG"
            SortExpression="ACTIVE_FLAG" UniqueName="ACTIVE_FLAG">
        </telerik:GridBoundColumn>
         
        <telerik:GridTemplateColumn HeaderText="Delete" UniqueName="Delete">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text="Delete" CommandName="Delete" />
                <asp:Button ID="Button2" runat="server" Text="Cancel" CommandName="Cancel" />
            </ItemTemplate>
        </telerik:GridTemplateColumn>
         
    </Columns>
 
    <PagerStyle AlwaysVisible="True" />
 
</MasterTableView>
 
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
    </telerik:RadGrid>
Protected Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand
    If e.CommandName = "Delete" Then
        Delete(sender, e)
    ElseIf e.CommandName = "Cancel" Then
        Cancel(sender, e)
    End If
 
End Sub
 
Protected Sub Delete(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs)
    Dim rowNum As Integer = Convert.ToInt32(e.Item.ItemIndex)
    Dim item As GridDataItem = RadGrid1.Items(rowNum)
    Dim lb As Label
 
    For Each row As GridDataItem In RadGrid1.Items
        lb = item.FindControl("Label1")
        If row.ItemIndex <> rowNum Then
            lb.Visible = False
        ElseIf row.ItemIndex = rowNum Then
            lb.Visible = True
        End If
    Next
End Sub
 
Protected Sub RadGrid1_NeedDataSource(sender As Object, e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
    Dim da As New SqlDataAdapter
    Dim con As New SqlConnection
    Dim com As New SqlCommand
    con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True"
    con.Open()
    com.Connection = con
    com.CommandText = "SELECT * FROM [area_zip_table]"
    da.SelectCommand = com
    Dim dt As New DataTable
    da.Fill(dt)
    RadGrid1.DataSource = dt
End Sub

4 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 17 Oct 2012, 04:55 AM
Hello,

Please try with below code snippet.
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "Delete1")
        {  
            GridDataItem item = e.Item as GridDataItem;
            Label Label1 = item.FindControl("Label1") as Label;
            Label1.Visible = true;
        }
    }


Thanks,
Jayesh Goyani
0
Singl Pang
Top achievements
Rank 1
answered on 17 Oct 2012, 06:59 AM
Hello Jayesh:
      According to your code, I modified my code as below:
    Protected Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles RadGrid1.ItemCommand  
        If e.CommandName = "Delete" Then
            Delete(sender, e)
        ElseIf e.CommandName = "Cancel" Then
            Cancel(sender, e)
        End If
End Sub
 
    Protected Sub Delete(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs)
        Dim item As GridItem = e.Item
        Dim lb As Label = item.FindControl("Label1")
        lb.Visible = True
    End Sub

but it still not fire. The label still un visible.
0
Accepted
Princy
Top achievements
Rank 2
answered on 17 Oct 2012, 07:10 AM
Hi,

Please try setting a different CommandName instead of default(Delete).

ASPX:
<asp:Button ID="Button1" runat="server" Text="Delete" CommandName="Delete1" />

VB:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs)
    If e.CommandName = "Delete1" Then
        Delete(sender, e)
    End If
End Sub
 
Protected Sub Delete(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs)
    Dim item As GridItem = e.Item
    Dim lb As Label = DirectCast(item.FindControl("Label1"), Label)
    lb.Visible = True
End Sub

Thanks,
Princy.
0
Singl Pang
Top achievements
Rank 1
answered on 17 Oct 2012, 08:50 AM
Pricy, Thank you very Much~! It's OK!~
Tags
Grid
Asked by
Singl Pang
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Singl Pang
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Share this question
or