Reputation: 49
Hi i am trying to add asp:LinkButton in specific div. but its not working. It just add text but link not showing. I don't know where i am wrong. Below is what i am trying to do.
<div class="form-group">
<div id="divTitle" runat="server">
//I am trying to add link button here
</div>
</div>
//Server side code is below
Int32 counter = 1;
divTitle.Controls.Clear();
foreach (DataRow dritem in dt.Rows)
{
divTitle.InnerHtml = "<asp:LinkButton ID='lbl" + counter++ + "' runat='server' CommandArgument='" + dritem["DocumentName"] + "' OnClick='ViewDocument' Visible='true'>" + dritem["UpDocumentName"] + "</asp:LinkButton>";
}
Upvotes: 1
Views: 3671
Reputation:
The problem is that you are trying to create an ASP control by adding it as inner html, but that ignores how a .NET server deals with asp pages and how your browser interprets the given page.
When you add InnerHTML to a div, in your server side code, that is taken by the .NET server as html code that it does not need to parse. But ASP controls have to be parsed into actual HTML tags so that your browser can recognize them.
Since it is not parsed, your browser gets a tag that says <asp:Link...
and there is no such tag in valid html.
So what you have to do is either add valid html as innerHTML, something like
divTitle.InnerHtml = "<a ID='lbl" + counter++ + ...
or add the ASP control as a created control. Since you are wanting to connect that control to a click event handler, you really need to do this second option
var MyLinkButton = new LinkButton();
MyLinkButton.ID = "lbl" + counter++;
MyLinkButton.Text = dritem["UpDocumentName"];
MyLinkButton.CommandArgument = dritem["DocumentName"];
MyLinkButton.Click += ViewDocument;
divTitle.Controls.Add(MyLinkButton);
Upvotes: 0
Reputation: 61
you can do this ...
<div class="form-group">
<div id="divTitle" runat="server">
//I am trying to add link button here
</div>
</div>
and in the server side ...
Control divTitleControl = this.Page.FindControl("divTitle");
LinkButton linkButton = new LinkButton();
divTitleControl.Controls.Add(linkButton);
Upvotes: 1
Reputation: 175
The string you are assigning to divTitle.InnerHtml is just a string. Try this: (or bind your data to repeater as suggested in comments - which would be the correct way I guess)
//....
var button = new LinkButton();
button.CommandArgument = dritem["DocumentName"];
button.Click += ViewDocument;
button.Text = dritem["UpDocumentName"];
divTitle.Controls.Add(button);
Upvotes: 0