DataGrid EditItemTemplate-- how to retrieve a DropDownList validator?

Discussion in 'ASP .Net Datagrid Control' started by Steve Hershoff, Apr 11, 2007.

  1. 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!
    Steve Hershoff, Apr 11, 2007
    #1
    1. Advertising

  2. RE: DataGrid EditItemTemplate-- how to retrieve a DropDownList validat

    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_SelectedIndexChanged"
    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!
    >
    >
    >
    Milosz Skalecki [MCAD], Apr 11, 2007
    #2
    1. Advertising

  3. Re: DataGrid EditItemTemplate-- how to retrieve a DropDownList validat

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

    -Steve
    Steve Hershoff, Apr 11, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Durzi

    DropDownList in EditItemTemplate

    George Durzi, Sep 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    8,455
    George Durzi
    Sep 27, 2004
  2. =?Utf-8?B?Tkg=?=
    Replies:
    2
    Views:
    29,278
    =?Utf-8?B?Tkg=?=
    Mar 27, 2006
  3. K B
    Replies:
    0
    Views:
    1,991
  4. Steve Hershoff
    Replies:
    2
    Views:
    799
    Steve Hershoff
    Apr 11, 2007
  5. rsw8n

    DropDownList in DataGrid EditItemTemplate Return Error

    rsw8n, Nov 26, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    190
    rsw8n
    Nov 28, 2003
Loading...

Share This Page