我有一个带有gridview的标签控件。我想使用页脚行添加新记录但是当我尝试保存记录时,我无法在文本框中找到该值。我已经设置了clientidmode = static,我也尝试过使用递归的findcontrol,但无济于事。请有人帮忙 感谢
<asp:UpdatePanel ID="pnl" runat="server" ChildrenAsTriggers="true">
<ContentTemplate>
<asp:TabContainer ID="TabDetails" runat="server" AutoPostBack="true" OnActiveTabChanged="TabDetails_ActiveTabChanged"
ActiveTabIndex="1">
<asp:TabPanel runat="server" ID="TabNotes" HeaderText="Notes" CssClass="tabinactive">
<ContentTemplate>
<asp:GridView ID="GrdNotes" ClientIDMode="Static" runat="server" AutoGenerateColumns="false"
Width="99%" OnRowEditing="GrdNotes_RowEditing" OnRowCancelingEdit="GrdNotes_RowCancelingEdit"
OnRowUpdating="GrdNotes_RowUpdating" OnRowDeleting="GrdNotes_RowDeleting" OnRowCreated="GrdNotes_RowCreated"
ShowFooter="false">
<Columns>
<asp:TemplateField HeaderText="Notes">
<ItemTemplate>
<asp:HiddenField ID="hfNotesID" runat="server" Value='<%# Bind("Notes_ID")%>' />
<asp:Label ID="LblNotes" runat="server" Text='<%# Bind("Notes")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtNotes" runat="server" Text='<%# Bind("Notes")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="TxtNewNotes" ClientIDMode="Static" runat="server" Width="300px"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Notes_Date" ReadOnly="True" DataFormatString="{0:dd/MM/yyyy}"
HeaderText="Date" />
<asp:BoundField DataField="FullName" ReadOnly="True" HeaderText="Entered By" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnAdd" runat="server" Text="Add Notes" OnClick="btnAddVisitNotes_Click" />
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
</ContentTemplate>
</asp:UpdatePanel>
private void BindNotes(int id)
{
var qry = from vn in dc.sp_list_notes(id)
orderby vn.Notes_Date descending
select vn;
GrdNotes.DataSource = qry.ToList();
GrdNotes.DataBind();
}
protected void GrdNotes_RowCreated(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
var lnk = new LinkButton();
lnk.Text = "Save";
lnk.ID = "btnAddNotesSave";
lnk.CausesValidation = false;
lnk.Command += new CommandEventHandler(btnAddNotesSave_Click);
lnk.CssClass = "norm";
e.Row.Cells[1].Controls.Add(lnk);
var lbl = new Label();
lbl.Text = "XX";
lbl.ID = "LblSpace";
lbl.CssClass = "norm_w";
e.Row.Cells[1].Controls.Add(lbl);
var lnk1 = new LinkButton();
lnk1.Text = "Cancel";
lnk1.ID = "btnAddNotesCancel";
lnk1.CausesValidation = false;
lnk1.Command += new CommandEventHandler(btnAddNotesCancel_Click);
lnk1.CssClass = "norm";
e.Row.Cells[1].Controls.Add(lnk1);
}
protected void btnAddNotes_Click(object sender, EventArgs e)
{
GrdNotes.ShowFooter = true;
BindNotes(int.Parse(hfID.Value));
}
protected void btnAddNotesSave_Click(object sender, EventArgs e)
{
TextBox txt = (TextBox)GrdNotes.FooterRow.FindControl("TxtNewNotes") ;
string sNotes = txt.Text;
}
答案 0 :(得分:1)
“无法在文本框中找到值”表示您可以通过TextBox
找到FindControl
,但它的Text
属性会返回String.Empty
?
文本框中肯定有文字,但它会返回“”。如果我 检查txt.id它返回“TxtNewNotes”但txt.text =“”
在!IsPostBack
致电BindNotes
之前,您可能忘记添加Page_Load
支票。
谢谢,是的我无法相信我忘记了!
即使是最有经验的ASP.NET开发人员有时也会忘记这一点;)
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
BindNotes();
}