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

set control text

7 Answers 45 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lisa
Top achievements
Rank 1
Lisa asked on 18 Jul 2013, 06:29 PM
I want to set the control text but don't know how to get a hold of the control that is within a radgrid. Can someone help?
I have another thread related to this but it was mixed with insert form. I thought it'd be better to have a different thread for it.

Also, RadButton1_Click() is not even invoked if there is RequiredFieldValidator and the field value is empty. How can RequiredFieldValidator be disabled when the button is clicked so that it invokes RadButton1_Click() ?

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
  <telerik:RadButton runat="server" ID="RadButton1" AutoPostBack="true" Text="fill data"  ButtonType="LinkButton" onclick="RadButton1_Click"></telerik:RadButton>
</div>
  
<telerik:RadAjaxPanel runat=server>
    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
            ShowStatusBar="True" GridLines="None"  OnNeedDataSource="RadGrid1_NeedDataSource"   OnUpdateCommand="RadGrid1_UpdateCommand"
            OnInsertCommand="RadGrid1_InsertCommand" OnCreateColumnEditor="RadGrid1_CreateColumnEditor"
            OnPreRender="RadGrid1_PreRender" OnItemDataBound="RadGrid1_ItemDataBound" OnItemCreated="RadGrid1_ItemCreated">
             
            <MasterTableView DataKeyNames="id" CommandItemDisplay="Top">
                <Columns>
                    <telerik:GridBoundColumn HeaderText="Emp" DataField="emp" UniqueName="emp">
  
 
protected void RadButton1_Click(object sender, EventArgs e)
{
        //set the text of emp to something else. How to get a hold of the control in here?
}

7 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 19 Jul 2013, 10:21 AM
Hello,

Please try with the below code snippet.

<telerik:RadButton runat="server" ID="RadButton1" AutoPostBack="true" Text="fill data"
        ButtonType="LinkButton" OnClick="RadButton1_Click" CausesValidation="false">
    </telerik:RadButton>

Let me know if any concern.

Thanks,
Jayesh Goyani
0
Lisa
Top achievements
Rank 1
answered on 19 Jul 2013, 01:59 PM
Thank you for a solution to the second problem. Can you help with the first problem too?
 I want to set the control text but don't know how to get a hold of the control that is within a radgrid inside RadButton1_Click() method.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 22 Jul 2013, 05:47 AM
Hello,

Please try with the below code snippet.

<telerik:RadButton runat="server" ID="RadButton1" AutoPostBack="true" Text="fill data in ALl row"
    ButtonType="LinkButton" OnClick="RadButton1_Click" CausesValidation="false">
</telerik:RadButton>
<telerik:RadButton runat="server" ID="RadButton2" AutoPostBack="true" Text="fill data in particuler row"
    ButtonType="LinkButton" CausesValidation="false" OnClick="RadButton2_Click">
</telerik:RadButton>
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView CommandItemDisplay="Top">
        <Columns>
            <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="emp" HeaderText="emp" UniqueName="emp">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    dynamic data = new[] {
           new { ID = 1, emp ="Name_1"},
           new { ID = 2, emp = "Name_2"},
           new { ID = 3, emp = "Name_3"},
           new { ID = 4, emp = "Name_4"},
           new { ID = 5, emp = "Name_5"}
       };
 
    RadGrid1.DataSource = data;
}
protected void RadButton1_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        item["emp"].Text = "your text comes here";
    }
}
protected void RadButton2_Click(object sender, EventArgs e)
{
    foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
    {
        if (item["ID"].Text == "1") // ID is column uniquename
        {
            item["emp"].Text = "your text comes here" + DateTime.Now.ToString();
        }
    }
 
}


Thanks,
Jayesh Goyani
0
Lisa
Top achievements
Rank 1
answered on 22 Jul 2013, 04:07 PM

It is my mistake that I did not post the RadTextBox correctly. It is not GridBoundColumn, and not inside <Columns>.

The RadTextBox is inside <MasterTableView>'s <FormTemplate>.

     <FormTemplate>
           <telerik:RadTextBox id="emp" runat="server" Text='<%# Bind("emp") %>'   />

So, how can I access the "emp" RadTextBox inside the FormTemplate?

0
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Jul 2013, 06:22 AM
Hello,

<telerik:RadButton runat="server" ID="RadButton1" AutoPostBack="true" Text="fill data in ALl row"
       ButtonType="LinkButton" OnClick="RadButton1_Click" CausesValidation="false">
   </telerik:RadButton>
   <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource">
       <MasterTableView CommandItemDisplay="Top" DataKeyNames="ID">
           <Columns>
               <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
               </telerik:GridBoundColumn>
               <telerik:GridBoundColumn DataField="emp" HeaderText="emp" UniqueName="emp">
               </telerik:GridBoundColumn>
               <telerik:GridEditCommandColumn>
               </telerik:GridEditCommandColumn>
           </Columns>
           <EditFormSettings EditFormType="Template">
               <FormTemplate>
                   <telerik:RadTextBox ID="txtemp" runat="server" Text='<%# Bind("emp") %>' />
               </FormTemplate>
           </EditFormSettings>
       </MasterTableView>
   </telerik:RadGrid>
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
 
 
        dynamic data = new[] {
           new { ID = 1, emp ="Name_1"},
           new { ID = 2, emp = "Name_2"},
           new { ID = 3, emp = "Name_3"},
           new { ID = 4, emp = "Name_4"},
           new { ID = 5, emp = "Name_5"}
       };
 
        RadGrid1.DataSource = data;
    }
 
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        foreach (GridDataItem item in RadGrid1.EditItems)
        {
            (item.EditFormItem.FindControl("txtemp") as RadTextBox).Text = "your text comes here";
        }
    }


Thanks,
Jayesh Goyani
0
Lisa
Top achievements
Rank 1
answered on 23 Jul 2013, 03:44 PM
It can find the control now. But because IsItemInserted was set to true, it displays 2 items: 2 "emp" text boxes, one with old text and the other with new text. I do not want it to display 2 items. I just want it to display only one item and replace the old text with the new text for that one item. How can that be done?

I also tried the following and it also displayed 2 items:
        GridEditableItem i = (GridEditableItem)RadGrid1.MasterTableView.GetInsertItem();
        RadTextBox r = (RadTextBox)i.FindControl("emp");
        r.Text = "Johnny";

I think with regular grid it is so simple, but with ragrid it is a different story.

protected void page_load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (data1.Tables[0].Rows.Count < 1)
        {
               RadGrid1.MasterTableView.IsItemInserted = true;
               RadGrid1.Rebind();
        }
       else
               RadGrid1.MasterTableView.IsItemInserted = false;
    }
}
0
Jayesh Goyani
Top achievements
Rank 2
answered on 24 Jul 2013, 06:20 AM
Hello,

Can you please provide your mark up/aspx page code?

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Lisa
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Lisa
Top achievements
Rank 1
Share this question
or