如何通过 Web 窗体上使用的 Web 用户控件在主页上获取文本框、日历和 DDL 的值
本文关键字:Web 获取 取文本 的值 DDL 主页 日历 控件 窗体 何通过 用户 | 更新日期: 2023-09-27 18:30:28
"我是 asp.net 新手,我无法从用户控件获取到主页的值。请帮助我,给我溶胶。我在 Web 窗体上创建了一个 Web 用户控件,并在用户控件中使用文本框、日历和 DDL,现在我想在主页上获取文本框、日历和 DDL 的值。以下是 ASCX 页面的代码
<asp:TextBox ID="txtData" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:Label ID="lblData" runat="server" ForeColor="Red">
</asp:Label>
<br />
<asp:Calendar ID="Calendar1" runat="server" BackColor="Beige" OnSelectionChanged="Calendar1_SelectionChanged"></asp:Calendar>
<br />
<asp:Label ID="Label1" runat="server" Font-Size="Large" ForeColor="Red"></asp:Label>
<br />
<asp:DropDownList ID="ddlthings" OnSelectedIndexChanged="ddlthings_SelectedIndexChanged" runat="server">
<asp:ListItem> Apple</asp:ListItem>
<asp:ListItem> Banana</asp:ListItem>
<asp:ListItem> Mango</asp:ListItem>
<asp:ListItem> Grapes</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="lblthings" ForeColor="Red" runat="server"></asp:Label>
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
//display the text in textbox
lblData.Text = txtData.Text;
}
protected void Calendar1_SelectionChanged(object sender, System.EventArgs e)
{
//display the selected date of calender
Label1.Text = "Your selected Date is: " +
Calendar1.SelectedDate.ToShortDateString();
}
protected void ddlthings_SelectedIndexChanged(object sender, EventArgs e)
{
//display the selected thing of dropdownlist
lblthings.Text = "Your selected Thing is: " + ddlthings.SelectedValue;
}
</script>
类似的东西
:
protected void Button1_Click(object sender, EventArgs e)
{
//display the text in textbox
lblData.Text = txtData.Text;
// retrive data from controls
var text = ((TextBox)txtData).Text;
var calendarValue = ((Calendar)Calendar1).SelectedDate;
var ddlValue = ((DropDownList)ddlthings).SelectedValue;
}