SelectedIndexChanged仅在表单提交后起作用。如果我从drpdown列表中选择数据,它会选择索引并将数据从数据库获取到texboxes。但是如果我单击Submit按钮,它将显示数据
我尝试使用回发,但是没有用。是否有解决此问题的替代方法。
HTML
<div class="col-md-4">
<asp:Label ID="Label10" runat="server" Text="Related Word"></asp:Label>
<asp:UpdatePanel ID="update" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtRelatedWord" runat="server" class="form-control" AutoPostBack="true" OnTextChanged="txtRelatedWord_TextChanged" placeholder="Word" ></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txtRelatedWord" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
<div class="col-md-4" style="margin-top:37px">
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ID="cmbRelatedWord" CssClass="form-control" AutoPostBack="true" OnSelectedIndexChanged="cmbRelatedWord_SelectedIndexChanged" runat="server" >
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="cmbRelatedWord" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
隐藏代码
private static void InsertRequest(ref RequestModel model, string connection)
{
using(var con = new SqlConnection(connection))
{
using(var cmd = new SqlCommand("Insert into infromationRequests (Gender, Age, IsMedicine, IsTransplant, Operations, ResponseJson) values (@Gender, @Age, @IsMedicine, @IsTransplant, @Operations, NULL); SELECT SCOPE_IDENTITY();", con))
{
cmd.Parameters.AddWithValue("@Gender", model.Gender);
cmd.Parameters.AddWithValue("@Age", model.Age);
cmd.Parameters.AddWithValue("@IsMedicine", model.Medicine);
cmd.Parameters.AddWithValue("@IsTransplant", model.Transplant);
cmd.Parameters.AddWithValue("@Operations", model.Operations);
con.Open();
model.Id = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
}
}
}
答案 0 :(得分:0)
好吧,我设法做了一个简单的测试,效果很好:
返回:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Console.WriteLine("Load triggered");
}
protected void ddlist_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("event triggered");
tb.Text = (sender as DropDownList).SelectedValue;
}
}
前面:
<asp:ScriptManager runat="server" ID="smanager"></asp:ScriptManager>
<asp:UpdatePanel runat="server" ID="upanel">
<ContentTemplate>
<asp:DropDownList ID="ddlist" runat="server" OnSelectedIndexChanged="ddlist_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
</asp:DropDownList>
<asp:TextBox runat="server" ID="tb" Text="Default"></asp:TextBox>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlist" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
据此,我可以为您提供这些线索: