Saturday, February 12, 2011

ListView with LINQ Datasource insert template

It seems like this should be straightforward but I'm boggling. I've got my listview all setup and bound to my LINQ datasource. The source is dependent on a dropdown list which decides which branch information to show in the listview. My edit template works fine but my insert template won't work because it wants the branch ID which I want to get from the dropdownlist outside the listview but I don't know how to both bind that value and set it in my template. It looks like this:

<InsertItemTemplate>
    <tr style="">
      <td>
         <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
               Text="Insert" />
     </td>
      <td>
       <asp:TextBox ID="RechargeRateTextBox" runat="server" 
          Text='<%# Bind("RechargeRate") %>' />
         </td>
              <td>
       <asp:Calendar SelectedDate='<%# Bind("StartDate") %>' ID="Calendar1"  runat="server"></asp:Calendar>                                    
       </td>
           </tr>
    </InsertItemTemplate>

I need to get a label in there that binds to the value of a databound asp dropdownlist outside of the listview so that the insert will work.

  • Use the OnSelectedIndexChanged (with AutoPostBack=True) callback for the DropDownList to manually set the values in the ListView to the defaults for that branch when the value of the DropDownList changes.

    protected void BranchDropDownList_OnSelectedIndexChanged( object sender, EventArgs e )
    {
        DropDownList ddl = (DropDownList)sender;
        RechargeRateTextBox.Text = BranchManager.GetRechargeRate( ddl.SelectedValue );
    }
    

    Wrap the whole thing up in an UpdatePanel and it can all happen via AJAX.

    From tvanfosson
  • I ended up going with this, thanks twanfosson.

    protected void ListView1_ItemInserting(object sender, System.Web.UI.WebControls.ListViewInsertEventArgs e)
            {
                e.Values["BranchID"] = DropDownList1.SelectedValue;
            }
    
    From Echostorm

0 comments:

Post a Comment