我在UpdatePanel做了一个dropdownlist和一个gridview,希望通过根据用户选择dropdownlist后自动显示gridview的值!之后在gridview中批量录入数据,然后提交数据库!现在问题是数据提交完成后,gridview无法自动刷新,就是取得dropdownlist的值为空值!为什么呢?应该怎么解决呢?查了一下午,不知道是不是查的方式不对,怎么也没有找到我想要的答案,肯请高手们支招,并希望能写得详细点!我比较菜啊!(C#语言)

解决方案 »

  1.   


         <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:GridView ID="GridView1" runat="server">
                </asp:GridView>
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
            </Triggers>
        </asp:UpdatePanel>
      

  2.   

    是不是DropDownList1_SelectedIndexChanged事件里没有写UpdatePanel1.Update();?
      

  3.   

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = DropDownList1.SelectedValue;
        UpdatePanel1.Update();
    }
      

  4.   

    吧gridview放在updatepanel中,使用Triggers的ControlID設為dropdownlist
      

  5.   

    不好意思,家里没有大网,所以两天没上!先谢谢大家了,下面是前台代码
    <body oncontextmenu="window.event.returnValue=false" style="text-align: center">
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdateProgress ID="UpdateProgress1" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
            <ProgressTemplate>
                <img src="Image/wait.gif" /><span style="color: #ff0000">数据更新中,请稍等......</span>
            </ProgressTemplate>
        </asp:UpdateProgress>&nbsp;
        <asp:UpdatePanel ID="UpdatePanel1" runat="server"><ContentTemplate>
    <SPAN style="COLOR: #0000ff"><BR /><TABLE style="WIDTH: 377px"><TBODY><TR><TD style="TEXT-ALIGN: center" colSpan=3>当前位置:员工劳保时间录入</TD></TR><TR><TD style="TEXT-ALIGN: center" colSpan=3><INPUT id="Hidden1" type=hidden /></TD></TR><TR><TD style="WIDTH: 147px; TEXT-ALIGN: right" colSpan=2>未录入劳保时间员工姓名:</TD><TD style="TEXT-ALIGN: left"><asp:DropDownList id="DDLsearch" runat="server" OnSelectedIndexChanged="DDLsearch_SelectedIndexChanged" AutoPostBack="True" __designer:wfdid="w2"></asp:DropDownList></TD></TR></TBODY></TABLE><BR /><yyc:SmartGridView id="SmartGridView1" runat="server" MouseOverCssClass="OverRow" AutoGenerateColumns="False" OnRowDataBound="SmartGridView1_RowDataBound" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" DataKeyNames="NAME,DM,HPDH" GridLines="None" MergeCells="0,1,2" OnPageIndexChanging="SmartGridView1_PageIndexChanging" PageSize="20" Width="750px">
    <FooterStyle BackColor="#C6C3C6" ForeColor="Black"></FooterStyle><FixRowColumn FixColumns="0,1" TableHeight="500px" FixRows="" FixRowType="Header,Pager" TableWidth="770px"></FixRowColumn><HeaderStyle BackColor="#4A3C8C" Height="25px" ForeColor="#E7E7FF"></HeaderStyle>
    <Columns>
    <asp:BoundField ReadOnly="True" DataField="NAME" SortExpression="NAME" HeaderText="员工姓名"></asp:BoundField>
    <asp:BoundField ReadOnly="True" DataField="DM" SortExpression="DM" HeaderText="所属队名"></asp:BoundField>
    <asp:BoundField DataField="GZMC" SortExpression="GZDH" HeaderText="工种名称"></asp:BoundField>
    <asp:BoundField ReadOnly="True" DataField="HPMC" SortExpression="HPDH" HeaderText="护品名称"></asp:BoundField>
    <asp:TemplateField SortExpression="YEARs" HeaderText="上次发放此护品时间"><EditItemTemplate>
    &nbsp;
    </EditItemTemplate>
    <itemTemplate>
    <asp:HiddenField id="HDFyear" runat="server" __designer:wfdid="w7" Value='<%# Eval("years") %>'></asp:HiddenField> <asp:TextBox style="BORDER-RIGHT: medium none; PADDING-RIGHT: 0px; BORDER-TOP: medium none; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; BORDER-LEFT: medium none; PADDING-TOP: 0px; BORDER-BOTTOM: #000000 1px solid" id="TextBox2" runat="server" Width="38px" __designer:wfdid="w8" MaxLength="4"></asp:TextBox> <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" __designer:wfdid="w9" ErrorMessage="只需录入年,如2009" ControlToValidate="TextBox2"></asp:RequiredFieldValidator> 
    </itemTemplate>
    </asp:TemplateField><asp:TemplateField HeaderText="下次发放时间" SortExpression="Nextyear">
    <ItemTemplate>
    <asp:Label id="Nextyear" runat="server" Text='<%# Bind("NextYear") %>' __designer:wfdid="w10"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns><PagerStyle BackColor="#C6C3C6"></PagerStyle><RowStyle BackColor="#DEDFDE"></RowStyle><SelectedRowStyle BackColor="#9471DE"></SelectedRowStyle>
    </yyc:SmartGridView> <BR /></SPAN><asp:Label id="Label1" runat="server" ForeColor="Red"></asp:Label> <BR /><asp:Button id="BtnAdd" onclick="BtnAdd_Click" runat="server" Text="保存新员工护品时间"></asp:Button> 
    </ContentTemplate>
     
    </asp:UpdatePanel>
        </form>
        </body>
      

  6.   

    后台代码
      private void DropDownListDataBind()
        {
            string IsYear = DateTime.Now.ToString().Substring(0, 4);
            string liststr;
            if (he.IsAdmin(Session["username"].ToString()))
            {
                liststr = "SELECT distinct a.name FROM emp_year a,division b,gzdhmc c,hpdhmc d,gzhpnxk e where a.dh=b.dh and a.gzdh=c.gzdh and a.hpdh=d.hpdh and a.gzdh=e.gzdh and a.hpdh=e.hpdh and a.nextyear is null order by name";
            }
            else
            {            string[] SelDiv = he.SelectDhKh(Session["username"].ToString());
                string Dh = SelDiv[1];
                liststr = "SELECT distinct a.name FROM emp_year a,division b,gzdhmc c,hpdhmc d,gzhpnxk e where a.dh=b.dh and a.gzdh=c.gzdh and a.hpdh=d.hpdh and a.gzdh=e.gzdh and a.hpdh=e.hpdh and a.nextyear is null and a.dh='" + Dh + "' order by name";
            }
            DDLsearch.DataSource = MyDb.DT(liststr);
            DDLsearch.DataTextField = "name";
            DDLsearch.DataValueField = "name";
            DDLsearch.DataBind();
            DDLsearch.Items.Insert(0, new ListItem("--请选择姓名--"));
        }    protected void DDLsearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(30);
            
            string IsYear = DateTime.Now.ToString().Substring(0, 4);
            SelectName = DDLsearch.SelectedValue.ToString();
            
            if (he.IsAdmin(Session["username"].ToString()))
            {
                str = "SELECT a.*,b.dm,c.gzMC,d.hpMC,a.years FROM emp_year a,division b,gzdhmc c,hpdhmc d,gzhpnxk e where a.dh=b.dh and a.gzdh=c.gzdh and a.hpdh=d.hpdh and a.gzdh=e.gzdh and a.hpdh=e.hpdh order by name";
            }
            else
            {            string[] SelDiv = he.SelectDhKh(Session["username"].ToString());
                string Dh = SelDiv[1];
                str = "SELECT a.*,b.dm,c.gzMC,d.hpMC,a.years FROM emp_year a,division b,gzdhmc c,hpdhmc d,gzhpnxk e where a.dh=b.dh and a.gzdh=c.gzdh and a.hpdh=d.hpdh and a.gzdh=e.gzdh and a.hpdh=e.hpdh and a.dh='" + Dh + "' and name='" + SelectName + "' order by name";
            }
            BtnAdd.Visible = true;
            SmartGridView1.DataSource = MyDb.DT(str);
            SmartGridView1.DataBind();
        }
     private void GridViewDataBind()
        {
            string IsYear = DateTime.Now.ToString().Substring(0, 4);
            if (he.IsAdmin(Session["username"].ToString()))
            {
                str = "SELECT a.*,b.dm,c.gzMC,d.hpMC,a.years FROM emp_year a,division b,gzdhmc c,hpdhmc d,gzhpnxk e where a.dh=b.dh and a.gzdh=c.gzdh and a.hpdh=d.hpdh and a.gzdh=e.gzdh and a.hpdh=e.hpdh order by name";
            }
            else
            {            string[] SelDiv = he.SelectDhKh(Session["username"].ToString());
                string Dh = SelDiv[1];
                str = "SELECT a.*,b.dm,c.gzMC,d.hpMC,a.years FROM emp_year a,division b,gzdhmc c,hpdhmc d,gzhpnxk e where a.dh=b.dh and a.gzdh=c.gzdh and a.hpdh=d.hpdh and a.gzdh=e.gzdh and a.hpdh=e.hpdh and a.dh='" + Dh + "' and name='" + SelectName + "' order by name";
            }
            SmartGridView1.DataSource = MyDb.DT(str);
            SmartGridView1.DataBind();
    }
    到GridViewDataBind中的SelectName 就为空值,为什么呢?
      

  7.   

    弄个项目发给我 我尽量帮你解决 ! [email protected]
      

  8.   

    先谢谢mamudelingling!只是我的数据库是oracle的,做项目也带不过去啊,我只把这个文件给你传了!
      

  9.   

    UpdatePanel1没有设置更新模式?建议把dropdownlist放置放在UpdatePanel1外面,设置UpdateMode="Conditional"属性,然后在SelectedIndexChanged函数里面调用UpdatePanel1.update()刷新。
      

  10.   

    CRM.Models.Dict dict = new CRM.Models.Dict();
    dict.Type = txtType.Text;
    dict.Item = txtItem.Text;
    dict.Value = txtValue.Text;
    dict.IsEditable = cbyesOrNo.Checked;
    DictManager.insertDict(dict);
    Response.Redirect("Dict.aspx");不知道对不对,如果不对,请指教!!!
    [email protected]