Question

Please check the below code, where I am able to generate the table dynamically from the database. But not able to display the link button inside the <td> element.

The basic function is to generate a new <tr> for every row in the database table with a link button added.

Aspx Code

<div style="width: 80%;" id="div_post" runat="server">
</div>

Aspx.cs Code

protected void GetvicharData()
{
    try
    {
        Data_display dd = new Data_display();
        DataTable dt = dd.disp_vichar();
        string in_html = string.Empty;
        int i = 0;
        in_html = "<table style=\"width: 100%;\">";
        foreach (DataRow dr in dt.Rows)
        {
        string str_build = string.Empty;
        i = i + 1;
        string lbDate = Convert.ToDateTime(dr["Date"]).ToString("dd-MMM-yy");
        string lbTopic = dr["Topic_Name"].ToString();
        string desc = dr["Description"].ToString();
        string imgURL = dr["img_url"].ToString();
        string textUrl = dr["txt_url"].ToString();
        str_build = ret_string(lbDate, lbTopic, desc, imgURL, textUrl, i);
        in_html += str_build;
        }
        in_html += "</table>";
        div_post.InnerHtml = in_html;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

public string ret_string(string lbldate, string lbltopic, string description, string imgurl, string texturl, int i)
{
    try
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<tr><td class=\"post_date\" valign=\"top\" align=\"center\">");
        sb.Append("<asp:Label ID=\"lblDate\" runat=\"server\">" + lbldate + "</asp:Label>");
        sb.Append("</td><td class=\"post_topic\" valign=\"top\" >");
        sb.Append(" <asp:Label ID=\"lblTopic" + i + "\" runat=\"server\">" + lbltopic + "</asp:Label>");
        sb.Append("</td></tr><tr>");
        sb.Append("<td class=\"ShowPic\" valign=\"top\" align=\"right\" ><img src=\"" + imgurl + "\" alt=\"\" id=\"img_post\" /></td>");
        sb.Append("<td class=\"ShowPost\" valign=\"top\" style=\"text-align: justify\">");
        sb.Append("<asp:Panel ID=\"pnlDesc" + i + "\" runat=\"server\"><p>" + description + "</p>");
        sb.Append("</asp:Panel>");
        sb.Append("<div><asp:LinkButton ID=\"lnkbtn" + i + "\" runat=\"server\" Text=\"Read more...\" onclick=\"lnkbtn1_Click\" OnClientClick=\"openNewWin('" + texturl + "')\" />");
        sb.Append("</asp:LinkButton></div></td></tr>");
        string sbuild = sb.ToString();
        return sbuild;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

As I am not able to figure it out that why my link button is showing hidden when I am rendering the page in the browser.

Was it helpful?

Solution 3

I got my answer. Please check the code.

sb.Append("<a href=\"#\" onclick=\"openNewWin('" + texturl + "')\" >Read More...</a>");

OTHER TIPS

OK didn't test but,

sb.Append("</asp:LinkButton></div></td></tr>");

Where is the opening for the last </tr> ? . It seems to me its missing.

try sb.Append("</asp:LinkButton></div></td>"); instead

Also if it persist , try removing the last and putting it in a <tr><td> instead. One thing to put in mind also . Your ret_string method is in a a loop and therefore returns 1 row at a time. You can copy your ret_string method to an asp.net page and remove the C# codings, test and see if you have a successful row returned. Goodluck.

Update Also

OnClientClick=\"openNewWin('" + texturl + "')\" />");

Cn you try OnClientClick=\"openNewWin('" + texturl + "')\" >"); instead since i noticed you close the linkbutton in the next line already. So try remove the /> and see what happened?

Sorry Man. Just now when I see your code again It looks strange. I had to put another answer for i don't have any means of testing here. Like i mentioned in my previous answer, Your ret_string method is in a loop . Therefore your str_build should hold row ++ or one row at each loop instance. When you do like this..

foreach (DataRow dr in dt.Rows)
        {
        string str_build = string.Empty;
        .................
        str_build = ret_string(lbDate, lbTopic, desc, imgURL, textUrl, i);
        in_html += str_build;
        }

First str_build; hold one row in the first going. However, when it comes another round, you set string str_build = string.Empty; , this automatically cleared whih cever row the str_build; is holding if i understand your code clear. I am not sure how you get your rows returned by , but i had suggest you take out the

string str_build = string.Empty;

and put it before your loop like below

 string str_build = string.Empty;
foreach (DataRow dr in dt.Rows)
{

        .................
        str_build = ret_string(lbDate, lbTopic, desc, imgURL, textUrl, i);
        in_html += str_build;
}

Goodluck . try and see man....

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top