gridview中的下拉列表选择后返回默认值

问题描述:

我有一个gridview下拉列表,也有一个linkbutton,按钮和面板。如下所示。当我在下拉列表中选择一个值时,它返回到默认值gridview中的下拉列表选择后返回默认值

我试图把gridview放在updatepanel中,并将dropdrownlist的enable viewstate设置为true,并且确实有帮助,请有任何建议。

<table align="center" cellpadding="0" cellspacing="0" class="box" 



     <tr> 
      <td colspan="4" class="pText" align="center"> 
       <b>Treatment Details<br /> 
       </b> 
       <asp:LinkButton ID="LinkButton6" runat="server" CssClass="grey1">Basic Billing</asp:LinkButton> 
</td> 
</tr> 
</table> 

    <asp:Panel ID="Panel4" runat="server" Visible="False"> 
<table align="center" cellpadding="0" cellspacing="0" 
     style="height: 88px; width: 294px"> 
    <tr> 
     <td align="right" class="grey1" colspan="2" style="height: 13px"> 
      .:Basic Billing</td> 
    </tr> 
    <tr> 
     <td> 
      &nbsp;</td> 
     <td> 
      &nbsp;</td> 
    </tr> 
<tr> 
     <td class="pText" style="height: 19px"> 
      Enter policy Number:</td> 
     <td style="height: 19px"> 
      <asp:TextBox ID="TextBox1" runat="server" CssClass="textfields">  </asp:TextBox> 

      </td> 
    </tr> 
    <tr> 
     <td class="pText" style="height: 19px"> 
      Enter Number of Basic Billing Item:</td> 
     <td style="height: 19px"> 
      <asp:TextBox ID="TextBox2" runat="server" CssClass="textfields"></asp:TextBox> 

     </td> 
    </tr> 

    <tr> 
     <td> 
      &nbsp;</td> 
     <td> 
      <asp:Button ID="Button1" runat="server" CssClass="textfields" Text="Submit" /> 
     </td> 
    </tr> 
    <tr> 
     <td colspan="2"> 
     <asp:UpdatePanel ID="UpdatePanel3" runat="server"> 
     <ContentTemplate> 

     <asp:GridView ID="GridView2" runat="server" 
       AllowSorting="True" AlternatingRowStyle-CssClass="alt" 
       AutoGenerateColumns="False" CellPadding="2" CssClass="MyGridView" 
       OnRowCommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated" 
       OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" 
       PagerStyle-CssClass="pgr" Width="750px"> 
       <Columns> 
        <asp:TemplateField HeaderText="S/N"> 
         <ItemTemplate> 
          <%# Container.DataItemIndex + 1 %> 
         </ItemTemplate> 
         <HeaderStyle HorizontalAlign="Center" /> 
         <ItemStyle HorizontalAlign="Center" /> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Item"> 
         <ItemStyle HorizontalAlign="Left" /> 
         <HeaderStyle HorizontalAlign="Left" /> 
         <ItemTemplate> 
          <asp:DropDownList ID="drpOutp" runat="server" CssClass="textfields" OnSelectedIndexChanged="drpOutp_SelectedIndexChanged" AutoPostBack="true" OnLoad="Load_OutpDetail"> 
          </asp:DropDownList> 
          <controlstyle bordercolor="LightGray" /> 
         </ItemTemplate> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Unit Price"> 
         <ItemStyle HorizontalAlign="Left" /> 
         <HeaderStyle HorizontalAlign="Left" /> 
         <ItemTemplate> 
          <asp:Label ID="lblUnit" runat="server"></asp:Label> 
          <controlstyle bordercolor="LightGray" /> 
         </ItemTemplate> 
         </asp:TemplateField> 

        <asp:TemplateField HeaderText="Quantity"> 
         <ItemStyle HorizontalAlign="Left" /> 
         <HeaderStyle HorizontalAlign="Left" /> 
         <ItemTemplate> 
          <asp:TextBox ID="txteffdate" runat="server" CssClass="textfields" Height="15px" Width="80px"></asp:TextBox> 
          <controlstyle bordercolor="LightGray" /> 
         </ItemTemplate> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="Total Price"> 
         <ItemStyle HorizontalAlign="Left" /> 
         <HeaderStyle HorizontalAlign="Left" /> 
         <ItemTemplate> 
          <asp:Label ID="lblTotalP" runat="server"></asp:Label> 
          <controlstyle bordercolor="LightGray" /> 
         </ItemTemplate> 
        </asp:TemplateField> 

       </Columns> 
       <HeaderStyle CssClass="tblPageHeaderII_new" /> 
       <AlternatingRowStyle BackColor="#F4F4F4" /> 
       <PagerStyle HorizontalAlign="Right" /> 
       <EmptyDataTemplate> 
        <asp:Label ID="Label2" runat="server" Text="No Data Returned !"></asp:Label> 
       </EmptyDataTemplate> 
      </asp:GridView> 

     </ContentTemplate> 
     </asp:UpdatePanel> 

     </td> 
    </tr> 
    <tr> 
     <td> 
      &nbsp;</td> 
     <td> 
      &nbsp;</td> 
    </tr> 

</table> 
</asp:Panel> 

背后

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click 
    Panel3.Visible = True 
End Sub 

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click 
    Try 
     Dim params() As SqlParameter = {New SqlParameter("@PolicyNumber", Trim(txtPolicy.Text)), _ 
            New SqlParameter("@IPAddress", Request.ServerVariables("REMOTE_ADDR")), _ 
            New SqlParameter("@PostedBy", Session("HMOUser"))} 

     Dim retValue As Data.DataRow = SqlHelper.ExecuteDataset(Connstring, CommandType.StoredProcedure, "HMO_Enrollee_CheckByPolicyNumber", params).Tables(0).Rows(0) 

     If retValue Is Nothing Then 

      lblmsg.Text = "Invalid Policy Number" 
      txtPolicy.Text = "" 

     Else 
      Session("Enr") = retValue.Table.Rows(0).Item("Enrollee_Number") 

Catch ex As Exception 
       HMO_BLL.WriteLog(ex.Message + ex.StackTrace) 
      End Try 


     End If 

    Catch ex As Exception 

     lblmsg.Text = "Invalid Policy Number" 
    End Try 

Dim nos As Integer() = New Integer(Convert.ToInt32(txtItemNo.Text) - 1) {} 
    For i As Integer = 0 To nos.Length - 1 
     nos(i) = i + 1 
    Next 
    GridView1.DataSource = nos 
    GridView1.DataBind() 

End Sub 

Public Sub Load_OutpDetail(ByVal sender As Object, ByVal e As EventArgs) 
    'If Not IsPostBack Then 


    Try 
     Dim ds As Data.DataSet = DAL.HMO_Outp_Detail_drpdown() 
     Dim Li As ListItem 
     Li = New ListItem 
     Li.Value = "0" 
     Li.Text = "-- Select --" 

     Dim drpOutp As DropDownList = DirectCast(sender, DropDownList) 

     drpOutp.AppendDataBoundItems = True 
     drpOutp.Items.Clear() 
     drpOutp.Items.Add(Li) 

     If Not ds Is Nothing And ds.Tables(0).Rows.Count > 0 Then 
      With drpOutp 
       .DataSource = ds.Tables(0) 
       .DataTextField = "OutpDetail" 
       .DataValueField = "OutpID" 
       .DataBind() 
       .SelectedIndex = 0 
      End With 
     Else 
      drpOutp.Enabled = False 

     End If 
     ds.Dispose() 

    Catch ex As Exception 
     HMO_BLL.WriteLog(ex.Message + ex.StackTrace) 
    End Try 
    'End If 
End Sub 
Protected Sub drpOutp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 
    Try 
     For Each gvRow As GridViewRow In GridView1.Rows 

      Dim drpOutp As DropDownList = DirectCast(gvRow.FindControl("drpOutp"), DropDownList) 
      Dim lblEffDate As Label = DirectCast(gvRow.FindControl("lblEffDate"), Label) 

      'Dim drpOutp As DropDownList = DirectCast(sender, DropDownList) 

      Dim params() As SqlParameter = {New SqlParameter("@OutpID", drpOutp.SelectedValue), _ 
              New SqlParameter("@ProviderID", HiddenField1.Value)} 
      Dim retValue As Data.DataRow = SqlHelper.ExecuteDataset(Connstring, CommandType.StoredProcedure, "HMO_Outp_Fetch_price", params).Tables(0).Rows(0) 
      lblEffDate.Text = retValue.Table.Rows(0).Item("Uprice") 
     Next 

    Catch ex As Exception 
     HMO_BLL.WriteLog(ex.Message + ex.StackTrace) 
    End Try 
End Sub 

代码谢谢


@Tim谢谢,这是真正有用的,我没有你所说的话下面的代码。但我需要一些数据绑定在同一个GridView中的标签,是我有SelectedIndexChanged事件处理程序。我仍然有一点挑战,我希望能够绑定d行中的标签上的数据,其中dropdownlist在哪里选择了一个值,但是我似乎只绑定到第一行上的标签,而不管下拉列表是什么选择。任何指针?

<table align="center" cellpadding="0" cellspacing="0" class="box" 



    <tr> 
     <td colspan="4" class="pText" align="center"> 
      <b>Treatment Details<br /> 
      </b> 
      <asp:LinkButton ID="LinkButton6" runat="server" CssClass="grey1">Basic Billing</asp:LinkButton> 
    </td> 
    </tr> 
    </table> 

     <asp:Panel ID="Panel4" runat="server" Visible="False"> 
<table align="center" cellpadding="0" cellspacing="0" 
    style="height: 88px; width: 294px"> 
<tr> 
    <td align="right" class="grey1" colspan="2" style="height: 13px"> 
     .:Basic Billing</td> 
</tr> 
<tr> 
    <td> 
     &nbsp;</td> 
    <td> 
     &nbsp;</td> 
</tr> 
<tr> 
      <td class="pText" style="height: 19px"> 
      Enter policy Number:</td> 
    <td style="height: 19px"> 
     <asp:TextBox ID="TextBox1" runat="server" CssClass="textfields">  </asp:TextBox> 

     </td> 
</tr> 
<tr> 
    <td class="pText" style="height: 19px"> 
     Enter Number of Basic Billing Item:</td> 
    <td style="height: 19px"> 
     <asp:TextBox ID="TextBox2" runat="server" CssClass="textfields"></asp:TextBox> 

    </td> 
</tr> 

<tr> 
    <td> 
     &nbsp;</td> 
    <td> 
     <asp:Button ID="Button1" runat="server" CssClass="textfields" Text="Submit" /> 
    </td> 
</tr> 
<tr> 
    <td colspan="2"> 
    <asp:UpdatePanel ID="UpdatePanel3" runat="server"> 
    <ContentTemplate> 

    <asp:GridView ID="GridView2" runat="server" 
      AllowSorting="True" AlternatingRowStyle-CssClass="alt" 
      AutoGenerateColumns="False" CellPadding="2" CssClass="MyGridView" 
      OnRowCommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated" 
      OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" 
      PagerStyle-CssClass="pgr" Width="750px"> 
      <Columns> 
       <asp:TemplateField HeaderText="S/N"> 
        <ItemTemplate> 
         <%# Container.DataItemIndex + 1 %> 
        </ItemTemplate> 
        <HeaderStyle HorizontalAlign="Center" /> 
        <ItemStyle HorizontalAlign="Center" /> 
       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Item"> 
        <ItemStyle HorizontalAlign="Left" /> 
        <HeaderStyle HorizontalAlign="Left" /> 
        <ItemTemplate> 
         <asp:DropDownList ID="drpOutp" runat="server" CssClass="textfields" OnSelectedIndexChanged="drpOutp_SelectedIndexChanged" AutoPostBack="true" OnLoad="Load_OutpDetail"> 
         </asp:DropDownList> 
         <controlstyle bordercolor="LightGray" /> 
        </ItemTemplate> 
       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Unit Price"> 
        <ItemStyle HorizontalAlign="Left" /> 
        <HeaderStyle HorizontalAlign="Left" /> 
        <ItemTemplate> 
         <asp:Label ID="lblUnit" runat="server"></asp:Label> 
         <controlstyle bordercolor="LightGray" /> 
        </ItemTemplate> 
        </asp:TemplateField> 

       <asp:TemplateField HeaderText="Quantity"> 
        <ItemStyle HorizontalAlign="Left" /> 
        <HeaderStyle HorizontalAlign="Left" /> 
        <ItemTemplate> 
         <asp:TextBox ID="txteffdate" runat="server" CssClass="textfields" Height="15px" Width="80px"></asp:TextBox> 
         <controlstyle bordercolor="LightGray" /> 
        </ItemTemplate> 
       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Total Price"> 
        <ItemStyle HorizontalAlign="Left" /> 
        <HeaderStyle HorizontalAlign="Left" /> 
        <ItemTemplate> 
         <asp:Label ID="lblTotalP" runat="server"></asp:Label> 
         <controlstyle bordercolor="LightGray" /> 
        </ItemTemplate> 
       </asp:TemplateField> 

      </Columns> 
      <HeaderStyle CssClass="tblPageHeaderII_new" /> 
      <AlternatingRowStyle BackColor="#F4F4F4" /> 
      <PagerStyle HorizontalAlign="Right" /> 
      <EmptyDataTemplate> 
       <asp:Label ID="Label2" runat="server" Text="No Data Returned !"></asp:Label> 
      </EmptyDataTemplate> 
     </asp:GridView> 

    </ContentTemplate> 
    </asp:UpdatePanel> 

    </td> 
</tr> 
<tr> 
    <td> 
     &nbsp;</td> 
    <td> 
     &nbsp;</td> 
</tr> 

代码背后

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
    Try 
     If e.Row.RowType = DataControlRowType.DataRow Then 


      Dim ctrl As Control = e.Row.FindControl("drpOutp") 


      If ctrl IsNot Nothing Then 
       Dim ddl As DropDownList = TryCast(ctrl, DropDownList) 


       Dim ds As Data.DataSet = DAL.HMO_Outp_Detail_drpdown() 
       Dim Li As ListItem 
       Li = New ListItem 
       Li.Value = "0" 
       Li.Text = "-- Select --" 



       ddl.AppendDataBoundItems = True 
       ddl.Items.Clear() 
       ddl.Items.Add(Li) 

       If Not ds Is Nothing And ds.Tables(0).Rows.Count > 0 Then 
        With ddl 
         .DataSource = ds.Tables(0) 
         .DataTextField = "OutpDetail" 
         .DataValueField = "OutpID" 
         .DataBind() 
         .SelectedIndex = 0 
        End With 
       Else 
        ddl.Enabled = False 

       End If 
       ds.Dispose() 
      End If 
     End If 

    Catch ex As Exception 
     HMO_BLL.WriteLog(ex.Message + ex.StackTrace) 
    End Try 

End Sub 
Protected Sub drpOutp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) 
    Try 
     Dim ddl As DropDownList = TryCast(sender, DropDownList) 

     For Each gvRow As GridViewRow In GridView1.Rows 

      Dim ctrl As Control = TryCast(gvRow.FindControl("drpOutp"), DropDownList) 
      If ctrl IsNot Nothing Then 

       Dim ddl1 As DropDownList = DirectCast(ctrl, DropDownList) 


       Dim params() As SqlParameter = {New SqlParameter("@OutpID", ddl.SelectedValue), _ 
               New SqlParameter("@ProviderID", HiddenField1.Value)} 
       Dim retValue As Data.DataRow = SqlHelper.ExecuteDataset(Connstring, CommandType.StoredProcedure, "HMO_Outp_Fetch_price", params).Tables(0).Rows(0) 
       Dim UnitP As Label = TryCast(gvRow.FindControl("lblUnit"), Label) 
       UnitP.Text = retValue.Table.Rows(0).Item("Uprice") 
       Exit For 
      End If 
     Next 


    Catch ex As Exception 
     HMO_BLL.WriteLog(ex.Message + ex.StackTrace) 
    End Try 
End Sub 

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click 
    Dim nos As Integer() = New Integer(Convert.ToInt32(txtItemNo.Text) - 1) {} 
    For i As Integer = 0 To nos.Length - 1 
     nos(i) = i + 1 
    Next 
    GridView1.DataSource = nos 
    GridView1.DataBind() 

End Sub 
+0

为什么使用DropDownList的onLoad事件?你应该使用Page_Load,并且只有在不是'Page.IsPostback'来将你的GridView绑定到它的数据源。然后使用GridView的RowDataBound事件将DropDownList绑定到它的DataSource。此外,您不需要在SelectedIndexChanged事件处理程序中迭代所有GridView行。发件人**是**已更改的DropDownList。 –

+0

@Tim谢谢你,这真的有帮助,我做了你说的代码如下。但我需要一些数据绑定在同一个GridView中的标签,是我有SelectedIndexChanged事件处理程序。我仍然有一点挑战,我希望能够绑定d行中的标签上的数据,其中dropdownlist在哪里选择了一个值,但是我似乎只绑定到第一行上的标签,而不管下拉列表是什么选择。任何指针? – Eazyg

+0

这种情况发生在大多数人身上...人们离开代码来绑定page_load中的gridview而没有任何条件来检查页面是否是回发...只有当页面不是回发时,您才能确保您绑定gridview。 – deostroll

不要使用Dropdown1_selected指数变化......

用户在aspx文件下....``

<asp:DropDownList ID="ddlClass" runat="server" SelectedValue='<%#Eval("ClassName") %>'> 
          <asp:ListItem></asp:ListItem> 
          <asp:ListItem>9</asp:ListItem> 
          <asp:ListItem>10</asp:ListItem> 
          <asp:ListItem>11</asp:ListItem> 
          <asp:ListItem>12</asp:ListItem> 
          </asp:DropDownList> 
         </EditItemTemplate> 
        </asp:TemplateField> 

CS类代码在后面的的GridView .. edit_event

DropDownList的ddlClassName =(DropDownList的)(DetailsGrid.Rows [索引2] .Cells [0] .FindControl( “ddlClass”));

string ClassNamevalue = ddlClassName.SelectedValue.Trim();