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

时间:2011-09-19 14:37:41

标签: asp.net gridview drop-down-menu postback autopostback

我在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行中的标签上绑定数据,其中d下拉列表中选择了一个值,但我似乎只绑定到第一行上的标签,而不管下拉列表是什么选择。任何指针??

<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

1 个答案:

答案 0 :(得分:0)

不要使用Dropdown1_selected Index更改...

用户如下面的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>

Gridview的edit_event中的CS类代码..

DropDownList ddlClassName =(DropDownList)(DetailsGrid.Rows [index2] .Cells [0] .FindControl(“ddlClass”));

string ClassNamevalue = ddlClassName.SelectedValue.Trim();