470,567 Members | 1,497 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,567 developers. It's quick & easy.

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 7179
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Assimalyst | last post: by
reply views Thread by Trevor Hartman | last post: by
reply views Thread by Jeff Reed | last post: by
7 posts views Thread by localhost | last post: by
1 post views Thread by Mohamed Zaki | last post: by
3 posts views Thread by Richard | last post: by
reply views Thread by Luis Esteban Valencia | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.