下拉选择不会正确更新文本框 [英] dropdown selection is not updating the textboxes correctly

查看:84
本文介绍了下拉选择不会正确更新文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个填充文本框的下拉列表。第一个下拉列表填充第二个下拉列表,第二个下拉列表填充文本框。第一个下拉列表具有活动类型,第二个下拉列表显示活动名称。然后,活动名称将填充只读文本框。当下拉选项发生变化时,我无法更改文本框。



I have two dropdownlist that populate the textboxes. The first dropdown populates the second one and the second one populates the textboxes. The first dropdown has activity type and bases on that second dropdownlist shows the activity name. The activity name then populates read only textboxes. I am having trouble getting the textboxes to change when the dropdown selection is changed.

<asp:Content ID="Content1" ContentPlaceHolderID="cphMainContent" Runat="Server">
    <h2>Activity Type<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </h2>
<table>
<tr>
<td>Activity Type:</td>
<td>
    <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" 

        onselectedindexchanged="DropDownList2_SelectedIndexChanged" 

        DataSourceID="EntityDataSource1" DataTextField="ACT_TypeName" 

        DataValueField="ACT_TypeID">
    </asp:DropDownList>
    <asp:EntityDataSource ID="EntityDataSource1" runat="server" 

        ConnectionString="name=PSEntities" DefaultContainerName="PSEntities" 

        EnableFlattening="False" EntitySetName="ACTIVITYTYPEs" 

        Select="it.[ACT_TypeID], it.[ACT_TypeName]">
    </asp:EntityDataSource>
</td>
</tr>
</table>
<br/>

<h3>Activity</h3>
<table>
<tr>
<td>Event Name</td>
<td>
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 

        onselectedindexchanged="DropDownList1_SelectedIndexChanged" 

        DataSourceID="SqlDataSource1" DataTextField="INV_ActivityName" 

        DataValueField="INV_ActivityID">
        </asp:DropDownList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

        SelectCommand="SELECT [INV_ActivityID], [INV_ActivityName] FROM [INVOLVEMENT] WHERE ([ACT_TypeID] = @ACT_TypeID)">
        <SelectParameters>
            <asp:ControlParameter ControlID="DropDownList2" Name="ACT_TypeID" 

                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <asp:TextBox ID="txtName" runat="server" Height="22px"></asp:TextBox>
    </td>
    <tr>
    <td>Event Date:</td>
    <td>
        <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>  </td>
    </tr>
    <tr>
    <td>Event Hours:</td>
    <td>
        <asp:TextBox ID="txtHours" runat="server"></asp:TextBox>  
    </td>
    </tr>
    <tr>
    <td>Description:</td>
    <td>
        <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" ></asp:TextBox>  
 </td>
 </tr>
 <tr>
 <td>
     <asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" /></td>
    </tr>
 </table>
 <br />
</asp:Content>





代码背后



Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PSISModel;

public partial class ActivityList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        int selectedValue = Convert.ToInt32(DropDownList1.SelectedValue);
        
        using (PSEntities myEntity = new PSEntities())
        {       
           var query = (from a in myEntity.INVOLVEMENTs
                         where a.INV_ActivityID== selectedValue
                         select a).SingleOrDefault();

           txtDate.Text = query.INV_EventDate.ToString();
           txtHours.Text = query.INV_Hours.ToString();
           txtDescription.Text = query.INV_BriefDescript;
            
        }
    }



    protected void btnAdd_Click(object sender, EventArgs e)
    {
  // this part works
        using (PSEntities myEntity = new PSEntities())
        {
            INVOLVEMENT studInvolvement = new INVOLVEMENT();

            studInvolvement.ACT_TypeID = Convert.ToInt32(DropDownList2.SelectedValue);
            studInvolvement.INV_ActivityName = txtName.Text;
            studInvolvement.INV_BriefDescript = txtDescription.Text;
            studInvolvement.INV_Hours = Convert.ToInt32(txtHours.Text);
            studInvolvement.INV_EventDate = Convert.ToDateTime(txtDate.Text);

            var user = new STUDENT { STUD_CWID = Convert.ToInt32(Session["CWID"]) };

            myEntity.STUDENTs.Attach(user);

            studInvolvement.STUDENTs.Add(user);           
            myEntity.SaveChanges();
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectedValue = Convert.ToInt32(DropDownList1.SelectedValue);

        using (PSEntities myEntity = new PSEntities())
        {

            var query = (from a in myEntity.INVOLVEMENTs
                         where a.INV_ActivityID == selectedValue
                         select a).SingleOrDefault();

            txtDate.Text = query.INV_EventDate.ToString();
            txtHours.Text =query.INV_Hours.ToString();
            txtDescription.Text = query.INV_BriefDescript;

        }


       

        if (DropDownList2.SelectedValue == "4")
        {
            DropDownList1.Visible = false;
            txtName.Visible = true;
            txtName.Text = null;
            txtName.Enabled = true;
            txtDate.Text = null;
            txtDate.Enabled = true;
            txtDescription.Text = null;
            txtDescription.Enabled = true;
            txtHours.Text = null;
            txtHours.Enabled = true;

        }
        else
        {
            DropDownList1.Visible = true;
            txtName.Visible = false ;
            txtDate.Enabled = false;
            txtHours.Enabled = false;
            txtDescription.Enabled = false;
            txtName.Enabled = false;
        }
            
        

    }


}

推荐答案

ConnectionStrings:ConnectionString%>

SelectCommand = SELECT [INV_ActivityID],[INV_ActivityName] FROM [INVOLVEMENT] WHERE([ACT_TypeID] = @ACT_TypeID) < span class =code-keyword>>
< SelectParameters >
< asp:ControlParameter ControlID = DropDownList2 名称 = ACT_TypeID

< span class =code-attribute> PropertyName = SelectedValue 类型 = Int32 / >
< / SelectParameters >
</asp :SqlDataSource>
<asp:TextBox ID=\"txtName\" runat=\"server\" Height=\"22px\"></asp:TextBox>
</td>
<tr>
<td>Event Date:< /td>
<td>
<asp:TextBox ID=\"txtDate\" runat=\"server\"></asp:TextBox>  </td>
</tr >
<tr>
<td>Event Hours:</td>
<td>
<asp:TextBox ID=\"txtHours\" runat=\"server\"& gt;</asp:TextBox>  
</td>
</tr>
<tr>
<td>Description:</td>
<td>
< asp:TextBox ID=\"txtDescription\" runat=\"server\" TextMode=\"MultiLine\" ></asp:TextBox>  
</td>
</tr>
<tr>
<td>
<asp:Button ID=\"btnAdd\" runat=\"server\" Text=\"Add\" onclick=\"btnAdd_Click\" /></td>
</tr>
</table>
<br />
</asp:Content>
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [INV_ActivityID], [INV_ActivityName] FROM [INVOLVEMENT] WHERE ([ACT_TypeID] = @ACT_TypeID)"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList2" Name="ACT_TypeID" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:TextBox ID="txtName" runat="server" Height="22px"></asp:TextBox> </td> <tr> <td>Event Date:</td> <td> <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>  </td> </tr> <tr> <td>Event Hours:</td> <td> <asp:TextBox ID="txtHours" runat="server"></asp:TextBox>   </td> </tr> <tr> <td>Description:</td> <td> <asp:TextBox ID="txtDescription" runat="server" TextMode="MultiLine" ></asp:TextBox>   </td> </tr> <tr> <td> <asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" /></td> </tr> </table> <br /> </asp:Content>





Code behind



Code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PSISModel;

public partial class ActivityList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    {

        int selectedValue = Convert.ToInt32(DropDownList1.SelectedValue);
        
        using (PSEntities myEntity = new PSEntities())
        {       
           var query = (from a in myEntity.INVOLVEMENTs
                         where a.INV_ActivityID== selectedValue
                         select a).SingleOrDefault();

           txtDate.Text = query.INV_EventDate.ToString();
           txtHours.Text = query.INV_Hours.ToString();
           txtDescription.Text = query.INV_BriefDescript;
            
        }
    }



    protected void btnAdd_Click(object sender, EventArgs e)
    {
  // this part works
        using (PSEntities myEntity = new PSEntities())
        {
            INVOLVEMENT studInvolvement = new INVOLVEMENT();

            studInvolvement.ACT_TypeID = Convert.ToInt32(DropDownList2.SelectedValue);
            studInvolvement.INV_ActivityName = txtName.Text;
            studInvolvement.INV_BriefDescript = txtDescription.Text;
            studInvolvement.INV_Hours = Convert.ToInt32(txtHours.Text);
            studInvolvement.INV_EventDate = Convert.ToDateTime(txtDate.Text);

            var user = new STUDENT { STUD_CWID = Convert.ToInt32(Session["CWID"]) };

            myEntity.STUDENTs.Attach(user);

            studInvolvement.STUDENTs.Add(user);           
            myEntity.SaveChanges();
        }
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectedValue = Convert.ToInt32(DropDownList1.SelectedValue);

        using (PSEntities myEntity = new PSEntities())
        {

            var query = (from a in myEntity.INVOLVEMENTs
                         where a.INV_ActivityID == selectedValue
                         select a).SingleOrDefault();

            txtDate.Text = query.INV_EventDate.ToString();
            txtHours.Text =query.INV_Hours.ToString();
            txtDescription.Text = query.INV_BriefDescript;

        }


       

        if (DropDownList2.SelectedValue == "4")
        {
            DropDownList1.Visible = false;
            txtName.Visible = true;
            txtName.Text = null;
            txtName.Enabled = true;
            txtDate.Text = null;
            txtDate.Enabled = true;
            txtDescription.Text = null;
            txtDescription.Enabled = true;
            txtHours.Text = null;
            txtHours.Enabled = true;

        }
        else
        {
            DropDownList1.Visible = true;
            txtName.Visible = false ;
            txtDate.Enabled = false;
            txtHours.Enabled = false;
            txtDescription.Enabled = false;
            txtName.Enabled = false;
        }
            
        

    }


}


try add this code in event Page_Load



try add this code in event Page_Load

protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.isposback)
     {
     }
}


Thanks ! The post back worked.
Thanks ! The post back worked.


这篇关于下拉选择不会正确更新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆