Formview control with table, controls in tr set to runat server do not insert
Here's an odd situation. In my FormView
control, I put a <table>
for layout. With the rows (<tr>
) I added ID
s and runat="server"
. The insert only saved some of the data, which I thought was odd. I looked to see why those f开发者_Python百科ields stored and others didn't, and the only difference was I hadn't added the ID
s or runat="server"
to the <tr>
s. Removing the "runat" allowed the insert to work normally.
I assume this has something to do with the way the rows are initialized versus when the control or FormView
is initialized. Any thoughts why this happens? For now, I'm removing the ID
s from the table rows.
You're on the right track in your thinking.
The FormView
only looks at top-level controls. If you embed one of your intended input controls inside of another server-side control, the FormView
doesn't see it (it's been hidden inside the outer control). Even in your code-behind you'd have to use the FindControl
method to get the input tag out of the server-side <tr>
Thus, if you had this:
<tr ID="row1" runat="server">
<asp:TextBox ID="myTB" runat="server">12</asp:TextBox>
</tr>
The only thing the FormView
sees is the top-level element (the <tr>
). You wouldn't even see the TextBox
in your codebehind unless you did this:
TextBox myTB = (TextBox)row1.FindControl("myTB");
So my suggestion would be to leave off the "runat" and "ID" tags from the <tr>
elements; since they are just for display purposes, you don't really need them at the server-side.
I hope that clears things up a little.
精彩评论