首先,请原谅我的英语。我用Textbox和AutoCompleteExtender控件创建了一个用户控件,它运行正常。现在,我想反映我的文本框类似于下拉列表。当我将用户控件保留在GridView中并通过隐藏字段访问该值时,我得到“0”值。如何在页面中读取隐藏的字段值?
UserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" Code File="UserControl.ascx.cs"
Inherits="UserControl" %> <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<script type="text/java script">
function DispValue(sender, e) {
alert(e.get_value() + " : user control");
document.getElementById(hiddenFieldName.Client ID).value = e.get_value();
}
</script>
<asp:Hidden Field ID="hdnValue" runat="server" Value="0" />
<asp:Text Box ID="txtName" runat="server" Text=""> </asp:Text Box>
<cc1:AutoCompleteExtender ID="ACEName" TargetControlID="txtName" runat="server"
CompletionInterval="10" MinimumPrefixLength="1" Service Method="Get Name"
Service Path="UserControlWebServices.asmx" OnClientItemSelected="DispValue">
</cc1:AutoCompleteExtender>
UserControl.ascx.cs
public partial class UserControl : System.Web.UI.UserControl
{
protected void page_load(object sender, EventArgs e)
{
ACEName.ContextKey = "1";
}
public String SelectedValue
{
get { return this.hdnValue.Value; }
}
public String SelectedText
{
get { return this.Name.Text; }
}
}
MyAspxPage.aspx
<%@ Register Src="~/UserControl.ascx" TagPrefix="puc" TagName="UserControl" %> <head runat="server">
<title></title> </head> <body>
<form id="form1" runat="server">
<asp:Script Manager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:Script Manager>
<asp:Grid View ID="gvPatient" runat="server" AutoGenerateColumns="false" OnDataBound="GridPatient_DataBound">
<Columns>
<asp:Template Field>
<Header Template>
Patient Name
</Header Template>
<Item Template>
<puc:UserControl ID="pucPatient1" runat="server" />
</Item Template>
</asp:Template Field> </Columns>
</asp:Grid View>
<asp:Button ID="btnSave" runat="server" OnClick="Save" Text="Save" OnClientClick="return StartUpload();" />
</form>
</body>
</HTML>
MyAspxPage.cs
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Columns.Add("col4");
dt.Columns.Add("col5");
dt.Columns.Add("col6");
if (Session["dt"] == null)
{
dt = AddRow(dt);
gvPatient.DataSource = dt;
gvPatient.DataBind();
Session["dt"] = dt;
//ViewState["dt"] = dt;
}
else
dt = (DataTable)Session["dt"];//ViewState["dt"];
}
}
private DataTable AddRow(DataTable dt)
{
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr[0] = "";
dr[1] = "";
dr[2] = "";
dr[3] = "";
dr[4] = "";
dr[5] = "";
dt.Rows.Add(dr);
}
return dt;
}
protected void GridPatient_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow item in gvPatient.Rows)
{
UserControl ptuc = (UserControl)item.FindControl("pucPatient1");
string id = ptuc.SelectedValue;
}
}
public void Save(object sender, EventArgs e)
{
foreach (GridViewRow item in gvPatient.Rows)
{
if (item.RowType == DataControlRowType.DataRow)
{
UserControl ptuc = (UserControl)item.FindControl("pucPatient1");
string id = ptuc.SelectedValue;//getting null value.
string patientName = ptuc.SelectedText;
}
} }
这就是我所做的一切。
我坚持这个请帮助我。
Sharanamma
答案 0 :(得分:0)
嗨朋友们 感谢您的宝贵回复,
实际上它的工作正常,但UserControl的变化很小 客户选择的事件方,我,e
UserControl.ascx 功能DispValue(发件人,e){
var source = sender.get_id();
var lastIndex = source.lastIndexOf(“_”);
var replacement = source.substring(0,lastIndex + 1);
var hdnId = replace +“hdnPatientNumber”;
document.getElementById(hdnId).value = e.get_value();
}UserControl.ascx.cs
public String SelectedValue {
get {return Request.Form [this.hdnPatientNumber.UniqueID]; }
}感谢大家,恭喜,Sharanamma。