I have my own method and I am trying to findcontrol on a control inside the GridTemplateColumn, so I am doing it outside of the events for the radGrid. Is this possible and if so, how?
Thanks!
Try with below code.
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:Label ID="Label1"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1"></asp:TextBox>
</EditItemTemplate>
</telerik:GridTemplateColumn>
...................
button1_click()
{
// for Normal mode
foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
{
Label Label1 = item.FindControl("Label1") as Label;
}
// for edit mode
foreach (GridDataItem item in RadGrid1.EditItems)
{
TextBox TextBox1 = item.FindControl("TextBox") as TextBox;
}
}
Thanks,
Jayesh Goyani
Related
My code:
<asp:DataList ID="datalist" runat="server" >
<ItemTemplate>
<asp:Textbox ID="Values" runat="server" type="text" />
</ItemTemplate>
</asp:DataList>
<asp:Button ID="Button1" runat="server" Text="SEND" OnClick="send" />
How could I get each Values ID elements of the DataList from code behind in C# ?
You loop all the items in the DataList and use FindControl to locate the TextBox.
protected void send(object sender, EventArgs e)
{
//loop all the items in the datalist
foreach (DataListItem item in datalist.Items)
{
//find the textbox with findcontrol
TextBox tb = item.FindControl("Values") as TextBox;
//do something with the textbox content
string value = tb.Text;
}
}
I try to multiselect values in listbox into template field, when it is in edit mode. Is there a way to do it?
I tried something like this. I add HiddenField to bind there a values and then w JS code I want to select selected values (I can do it but I dont have a class in HiddenField so it isn't a good solution).
Also I don't know is there a chance to do it by server side becasue it didn't recognize Id's of mine Listbox
<asp:TemplateField HeaderText="Conditions" ItemStyle-Width="100px">
<EditItemTemplate>
<asp:HiddenField runat="server" ID="hdn_dd_conditions" class="bottom" Value='<%# Bind("conditions") %>' />
<asp:ListBox ID="dd_conditions" runat="server" CssClass="conditions" SelectionMode="Multiple" multiple="multiple">
<asp:ListItem Value="Value1" Text="Text2"></asp:ListItem>
<asp:ListItem Value="Value2" Text="Text2"></asp:ListItem>
<asp:ListItem Value="Others" Text="Others"></asp:ListItem>
</asp:ListBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("conditions") %>' Font-Size="10px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Any ideas?
You need to access the ListBox in the RowDataBound event of the GridView
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow and in edit mode
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) > 0)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//use findcontrol to locate the listbox and cast it
ListBox lb = e.Row.FindControl("dd_conditions") as ListBox;
//loop all the items in a listbox
for (int i = 0; i < lb.Items.Count; i++)
{
//check the values and set the Selected property
if (lb.Items[i].Value == row["conditions"].ToString())
{
lb.Items[i].Selected = true;
}
}
}
}
i have a Textbox inside ItemTemplate of a Gridview.. i need to find that textbox without using RowDatabound or any other event of Gridview
aspx
<asp:GridView ID="gv_Others" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate >
<asp:TextBox ID="txtEmp" runat="server" Width=100% Height=22px CssClass="input"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs
protected void Insert_OtherServices()
{
dsJobCardTableAdapters.Select_OtherServiceTableAdapter dsother = new dsJobCardTableAdapters.Select_OtherServiceTableAdapter();
int count = 0;
foreach (GridViewRow row in gv_Others.Rows)
{
//string b = (row.Cells[0].Controls[0] as DataBoundLiteralControl).Text;
//TextBox a = gv_Others.Rows[count].Cells[0].FindControl("txtEmp") as TextBox;
// string test = a.Text;
//TextBox other = (TextBox)gv_Others.Rows[count].FindControl("txtEmp");
TextBox other = (TextBox)gv_Others.TemplateControl.FindControl("txtEmp");
dsother.Insert_OtherService(other.Text);
count++;
}
}
is there any alternative to get the value from textbox which is inside the item template of Gridview
Do you mean that you want to find text box values without looping through GridView rows? If is is the case, jQuery/javascript can be used to achieve this.
Read all text box values on the client-side. You can find more information on how to do this here
Pass these values along with the postback request using a hidden field. Read and use the hidden field value inside your method.
I have created a DataList in asp.net -
<asp:DataList runat="server" ID="pTextBox">
<ItemTemplate>
<asp:CheckBox ID="CheckBoxPN" runat="server" Checked='false' />
<asp:TextBox ID="profileTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
This creates checkBoxes and textBoxes based on the string values passed through from a webService.
How can I get the profileTextBox Text string value when a user clicks CheckBoxPN and populate another textBox outwith the DataList on the page with the string value??
You can use the CheckedChanged event of the CheckBox and cast it's NamingContainer to DataListItem, the you just have to use FindControl to find a different server control:
protected void CheckBoxPN_CheckedChanged(Object sender, EventArgs e)
{
CheckBox chk = (CheckBox) sender;
DataListItem item = (DataListItem) chk.NamingContainer;
TextBox txt = (TextBox) item.FindControl("profileTextBox");
this.OtherTextBoxOnPage.Text = txt.Text; // here we are
}
By the way, this approach works with any web-databound control(Repeater, GridView, etc.)
I have textbox code in .aspx file as below
<telerik:GridTemplateColumn UniqueName="Format" HeaderText="Format" DataField="SystemMessageTextId" HeaderStyle-Width="60">
<ItemTemplate>
<asp:Label runat="server" ID="lblFormat" Text='<%# Eval("Format") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtFormat" Text='<%# Bind("Format") %>' MaxLength="255"></asp:TextBox>
<span style="color: Red">*
<asp:RequiredFieldValidator ID="rfFormat" runat="server" ErrorMessage = "Please enter a valid Format value." ControlToValidate="txtFormat" Display="Dynamic"></asp:RequiredFieldValidator>
</span>
</EditItemTemplate>
</telerik:GridTemplateColumn>
The .aspx.cs file looks like below
if ((e.Item is GridEditableItem) && (e.Item.IsInEditMode))
{
GridEditableItem edititem = (GridEditableItem)e.Item;
((TextBox)(edititem["Format"].FindControl("txtFormat"))).Text = "pdf";
}
I have this part in GridItemCreated function, but when I try it, the default value won't show. Can someone help me with this?
Thanks
Please try with the below code snippet.
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem item = e.Item as GridEditableItem;
TextBox txtFormat = (item.FindControl("txtFormat") as TextBox);
txtFormat.Text = "Your text";
}
}
If you want to assign/access data then its better to use ItemDataBound event.
Suppose you want assign textchanged event to Textbox then if we have to use ItemCreated event.
Please put Text="0" in your textbox in the gridview to set default value 0.