473,395 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DataGrid EditItemTemplate-- how to retrieve a DropDownList validator?

Hi everyone,

I have a DataGrid with several TemplateColumns. One of these columns has an
EditItemTemplate that contains an ASP.Net DropDownList. I'm catching this
DropDownList's SelectedIndexChanged event.

I also have another TemplateColumn, with a RequiredFieldValidator that
validates a TextBox in the column. It all looks like this, in broad
strokes:

<TemplateColumn>
<EditItemTemplate>
<asp:DropDownList OnSelectedIndexChanged="some method">
</asp:DropDownList>
</EditItemTemplate>
</TemplateColumn>
<TemplateColumn>
<EditItemTemplate>
<asp:TextBox id="textbox1">
</asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="textbox1">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</TemplateColumn>

When I'm in the method that catches the SelectedIndexChanged event, can I
somehow retrieve the RequiredFieldValidator and set its properties? I've
been noodling with FindControl() in various incarnations but haven't found
the right mix yet. Any help or snippets are appreciated.

Thanks!
Apr 11 '07 #1
2 7299
Hi Steve,

I created an example to show you how to do it:

-- beging aspx code --

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}

private void BindData()
{
dg.DataSource = new int[7];
dg.DataBind();
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = dg.EditItemIndex;
if (index >= 0)
{
DataGridItem item = dg.Items[index];

RequiredFieldValidator validator = (RequiredFieldValidator)
item.FindControl("rfv");
TextBox textBox = (TextBox) item.FindControl("textBox1");
}
}
protected void dg_EditCommand(object source, DataGridCommandEventArgs e)
{
((DataGrid) source).EditItemIndex = e.Item.ItemIndex;
BindData();
}
</script>

<asp:DataGrid runat="server" ID="dg" AutoGenerateColumns="false"
OnEditCommand="dg_EditCommand">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
column1
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList runat="server" ID="ddl"
OnSelectedIndexChanged="DropDownList1_SelectedInde xChanged"
AutoPostBack="true">
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
column2
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="textbox1" runat="server">
</asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="textbox1" runat="server"
ID="rfv">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn CommandName="Edit" Text="Edit" />
</Columns>
</asp:DataGrid>

-- end aspx code --

hope this helps

Milosz
"Steve Hershoff" wrote:
Hi everyone,

I have a DataGrid with several TemplateColumns. One of these columns has an
EditItemTemplate that contains an ASP.Net DropDownList. I'm catching this
DropDownList's SelectedIndexChanged event.

I also have another TemplateColumn, with a RequiredFieldValidator that
validates a TextBox in the column. It all looks like this, in broad
strokes:

<TemplateColumn>
<EditItemTemplate>
<asp:DropDownList OnSelectedIndexChanged="some method">
</asp:DropDownList>
</EditItemTemplate>
</TemplateColumn>
<TemplateColumn>
<EditItemTemplate>
<asp:TextBox id="textbox1">
</asp:TextBox>
<asp:RequiredFieldValidator ControlToValidate="textbox1">
</asp:RequiredFieldValidator>
</EditItemTemplate>
</TemplateColumn>

When I'm in the method that catches the SelectedIndexChanged event, can I
somehow retrieve the RequiredFieldValidator and set its properties? I've
been noodling with FindControl() in various incarnations but haven't found
the right mix yet. Any help or snippets are appreciated.

Thanks!
Apr 11 '07 #2
Thanks very much Milosz. This looks like just what I need.

-Steve
Apr 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kins | last post by:
Hi, In web application, I use listbox in the EditItemTemplate in the Datagrid. But I cannot load data from the database to the Listbox. Is it possible ? Thx a lot regards, Kin
6
by: Assimalyst | last post by:
Hi, I'm attempting to write a piece of code that acts as a client side validator for all drop down lists on a webform, giving an error if the SelectedIndex = 0. Here is what i have:
0
by: Trevor Hartman | last post by:
Hi, I'm using an EditItemTemplate on my datagrid for a date column in the DB. How would I go about using EditItemTemplate to split the date into three drop downs for day / month / year? Thanks -...
0
by: Jeff Reed | last post by:
I have this in my coding for a DataGrid: <EditItemTemplate> <asp:DropDownList id="eCategoryList" runat="server" CssClass="selectBoxSmall" SelectedIndex='<%# DataBinder.Eval(Container.DataItem),...
7
by: localhost | last post by:
A DataGrid with shows a label in one of the columns when in view mode. When in edit mode, I want to show a dropdown, and have the default selection set to what the textbox used to be. Right now...
1
by: Mohamed Zaki | last post by:
Dear All, I've a datagrid in its footer i added a drop down list and some text boxes and a link button to add new records in the database, however i populate the dropdown list in the ItemDataBind,...
3
by: Richard | last post by:
I've seen articles on GotDotNet and elsewhere on how to put a ddl in a datagrid, and have been able to implement this technique. For a new item, among the datagrid columns there is the one ddl for...
0
by: Luis Esteban Valencia | last post by:
Hello. I have a datagrid with one row. I have a button that adds a new row. I am trying to implement that when the user selects one product it must change the price on the quantity column. Anyway...
1
by: needin4mation | last post by:
I have a dropdownlist that I want to default to the proper value in the list. It should be what the current value of the label is before I press edit. Any ideas?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.