|
-
Mar 5th, 2009, 10:52 AM
#1
Thread Starter
Lively Member
Forming server tag error
Hi,
I am trying to form a div server tag in griview using the follwoing code.
Code:
<asp:GridView ID="gdv1" runat="server" AutoGenerateColumns="false" Visible="true" onrowdatabound="gdv1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Edit " HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<img src="Images/pencil.gif" alt=" " />
<asp:LinkButton ID="lnkBtnEditSymptom" runat="server" Text="[Edit]" OnClick="lnkBtnEditSymptom_Click"></asp:LinkButton>
</ItemTemplate>
<ItemStyle CssClass="labeltext" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<div id="div<%# Eval("SymptomUniqueId") %>" runat="server"style="display:none;position:relative;left:15px;OVERFLOW: auto;WIDTH:97%"></div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
When I remove runat="server" from div tag my page works fine and html div is created with the id and everything works fine. But when i add runat="server" the page errors out saying "The server tag if not well formed".
Any help pleaase...
-
Mar 5th, 2009, 10:55 AM
#2
Re: Forming server tag error
When doing inline code within a control, it needs to be wrapped in single quotes, not double, and the entire expression should be in the server tags, not part of it:
Code:
<div id='<%# "div" + Eval("SymptomUniqueId") %>' runat="server" style="display:none;position:relative;left:15px;OVERFLOW: auto;WIDTH:97%"></div>
-
Mar 5th, 2009, 11:06 AM
#3
Thread Starter
Lively Member
Re: Forming server tag error
Thanks for the reply. I tried this and got the following error:
Parser Error Message: The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />
-
Mar 5th, 2009, 11:54 AM
#4
Re: Forming server tag error
I guess that means you can't have the ID as a server-generated property, then... oh well. That's what HTML injection is for 
Be warned that while this is an extraordinarily dynamic solution that provides you with incredible power, it's also very precise, and needs to be done right, or the affected pages will render in impressively wrong ways. I've done it a few times, so stick to the basics and you should be fine.
Replace that div with an <asp:Literal> control, give it a static ID and runat="server". Then create the RowDataBound event if it isn't already. Everything from here happens in the code-behind (forgive my C#... untested, but based on code I've written a dozen times):
Code:
protected void gdv1_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = (DataRowView)e.Row.DataItem;
Literal myLit = (Literal)e.Row.FindControl("myLiteral");
myLit.Text = "<div id=\"div" + drv["SymptomUniqueId"].ToString() + "\" runat=\"server\" style=\"display:none;position:relative;left:15px;OVERFLOW: auto;WIDTH:97%\"></div>";
}
}
Essentially, this is determining the entire HTML output of the div before actually creating it, and Literals render exactly what's in their Text property and nothing else, so your resulting markup will look the same.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|