Visual Studio .NET - Pass from TemplateField to ObjectDataSource?

Asked By Daniel Schaffer on 31-May-06 04:33 PM
I've got an ObjectDataSource populating a GridView control. One of the fields in the gridview is a TemplateField with a dropdownlist in the EditItemTemplate. How do I get the selected value from the dropdownlist to the ObjectDataSource to use as a parameter for a command?

Found it

Asked By Daniel Schaffer on 31-May-06 05:11 PM
..in the SDK:
[CODE]
<%@ Page language="C#" %>
<script runat="server">
  void AuthorsGridView_RowUpdating (Object sender, GridViewUpdateEventArgs e)
  {
    // The GridView control does not automatically extract updated values 
    // from TemplateField column fields. These values must be added manually 
    // to the NewValues dictionary.
    // Get the GridViewRow object that represents the row being edited
    // from the Rows collection of the GridView control.
    int index = AuthorsGridView.EditIndex;
    GridViewRow row = AuthorsGridView.Rows[index];
    // Get the controls that contain the updated values. In this
    // example, the updated values are contained in the TextBox 
    // controls declared in the edit item templates of each TemplateField 
    // column fields in the GridView control.
    TextBox lastName = (TextBox)row.FindControl("LastNameTextBox");
    TextBox firstName = (TextBox)row.FindControl("FirstNameTextBox");
    // Add the updated values to the NewValues dictionary. Use the
    // parameter names declared in the parameterized update query 
    // string for the key names.
    e.NewValues["au_lname"] = lastName.Text;
    e.NewValues["au_fname"] = firstName.Text;    
  }
</script>
<html>
  <body>
    <form runat="server">
      <h3>TemplateField EditItemTemplate Example</h3>
      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames attribute as read-only.   -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="AuthorsGridView" 
        datasourceid="AuthorsSqlDataSource" 
        autogeneratecolumns="false"
        autogenerateeditbutton="true"
        datakeynames="au_id"
        cellpadding="10"
        onrowupdating="AuthorsGridView_RowUpdating"      
        runat="server">
        <columns>
          <asp:boundfield datafield="au_id"
            headertext="Author ID"
            readonly="true"/>
          <asp:templatefield headertext="Last Name"
            itemstyle-verticalalign="Top">
            <itemtemplate>
              <%#Eval("au_lname")%>
            </itemtemplate>
            <edititemtemplate>
              <asp:textbox id="LastNameTextBox"
                text='<%#Eval("au_lname")%>'
                width="90"
                runat="server"/>
              <br/>
              <asp:requiredfieldvalidator id="LastNameRequiredValidator"
                controltovalidate="LastNameTextBox"
                display="Dynamic"
                text="Please enter a last name." 
                runat="server" />                                      
            </edititemtemplate>
          </asp:templatefield>
          <asp:templatefield headertext="First Name"
            itemstyle-verticalalign="Top">
            <itemtemplate>
              <%#Eval("au_fname")%>
            </itemtemplate>
            <edititemtemplate>
              <asp:textbox id="FirstNameTextBox"
                text='<%#Eval("au_fname")%>'
                width="90"
                runat="server"/>
              <br/>
              <asp:requiredfieldvalidator id="FirstNameRequiredValidator"
                controltovalidate="FirstNameTextBox"
                display="Dynamic"
                text="Please enter a first name."
                runat="server" />                      
            </edititemtemplate>
          </asp:templatefield>
          <asp:checkboxfield datafield="contract" 
            headertext="Contract"
            readonly="true"/>
        </columns>
      </asp:gridview>
      <!-- This example uses Microsoft SQL Server and connects -->
      <!-- to the Pubs sample database.                         -->
      <asp:sqldatasource id="AuthorsSqlDataSource"  
        selectcommand="SELECT [au_id], [au_lname], [au_fname], [contract] FROM [authors]"             
        updatecommand="UPDATE authors SET au_lname=@au_lname, au_fname=@au_fname WHERE (authors.au_id = @au_id)" 
        connectionstring="server=localhost;database=pubs;integrated security=SSPI"
        runat="server">
      </asp:sqldatasource>
    </form>
  </body>
</html>
[/CODE]

GridView SelectedValue

Asked By F Cali on 31-May-06 05:12 PM
Just map the parameter of the command to the GridView1.SelectedValue and it will know that the value will be from the dropdownlist selected value.

Could you explain that in code?

Asked By Daniel Schaffer on 31-May-06 05:14 PM
Because you make it seem a lot easier than what I just posted
Sample code
Asked By Ramya T on 31-May-06 05:35 PM
Code for your dropdown sample. 
[CODE]
void AuthorsGridView_RowUpdating (Object sender, GridViewUpdateEventArgs e)   
{ 
int index = AuthorsGridView.EditIndex;    
GridViewRow row = AuthorsGridView.Rows[index]; 
TextBox lastName = (TextBox)row.FindControl("LastNameTextBox"); 
DropDownList StateName = (DropDownList)row.FindControl("state_edit");
e.NewValues["au_lname"] = lastName.Text; 
e.NewValues["state_name"] = StateName.SelectedItem.Value.ToString();
}
[/CODE]
[CODE]
<asp:gridview id="AuthorsGridView"  runat="server"....> 
<columns>                    
<asp:templatefield headertext="Last Name">
<itemtemplate>               
<%#Eval("state_name")%>             
</itemtemplate>                          
<edititemtemplate> 
<asp:DropDownList ID="state_edit" runat="Server">
       <asp:ListItem text="Tea" Value="1"  />
       <asp:ListItem text="chai" Value="2" />
</asp:DropDownList>                                                                 </edititemtemplate>          
</asp:templatefield> 
</columns>
</asp:gridview>
[/CODE]