HighTechTalks DotNet Forums  

DataGrid EditItemTemplate-- how to retrieve a DropDownList validator?

ASP.net Data Grid Control microsoft.public.dotnet.framework.aspnet.datagridcontrol


Discuss DataGrid EditItemTemplate-- how to retrieve a DropDownList validator? in the ASP.net Data Grid Control forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Steve Hershoff
 
Posts: n/a

Default DataGrid EditItemTemplate-- how to retrieve a DropDownList validator? - 04-10-2007 , 08:42 PM






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>
<aspropDownList OnSelectedIndexChanged="some method">
</aspropDownList>
</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!



Reply With Quote
  #2  
Old   
Milosz Skalecki [MCAD]
 
Posts: n/a

Default RE: DataGrid EditItemTemplate-- how to retrieve a DropDownList validat - 04-11-2007 , 09:44 AM






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>

<aspataGrid runat="server" ID="dg" AutoGenerateColumns="false"
OnEditCommand="dg_EditCommand">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
column1
</ItemTemplate>
<EditItemTemplate>
<aspropDownList runat="server" ID="ddl"
OnSelectedIndexChanged="DropDownList1_SelectedInde xChanged"
AutoPostBack="true">
<asp:ListItem Text="1" />
<asp:ListItem Text="2" />
<asp:ListItem Text="3" />
</aspropDownList>
</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>
</aspataGrid>

-- end aspx code --

hope this helps

Milosz


"Steve Hershoff" wrote:

Quote:
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
aspropDownList OnSelectedIndexChanged="some method"
/aspropDownList
/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!




Reply With Quote
  #3  
Old   
Steve Hershoff
 
Posts: n/a

Default Re: DataGrid EditItemTemplate-- how to retrieve a DropDownList validat - 04-11-2007 , 04:12 PM



Thanks very much Milosz. This looks like just what I need.

-Steve



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.