-3

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.

rene
  • 41,474
  • 78
  • 114
  • 152
apporv
  • 1
  • 4

3 Answers3

0

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?

Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
  • I checked in the aspx code again to be sure, and its working fine. I have also corrected the code as you have rightly pointed out. please check if anything i missed out. – apporv Nov 18 '13 at 06:00
  • The problem still exist. since i dont have enough repo. I cannot post the image here. – apporv Nov 18 '13 at 06:33
  • Then unaccept the answer so that some one else may help. In case if i get free time will run and see also. May update my answer after that. But for now unaccepted the answer , some one else may have a different opinion. Goodluck – Nuru Salihu Nov 18 '13 at 06:46
  • Also, can u suggest any method through which I can find the ID of the div without making it a server control. I referring to __ID= div_post__ – apporv Nov 18 '13 at 06:47
  • Ok sure will see. Pls check my answer i updated something i just noticed . – Nuru Salihu Nov 18 '13 at 06:53
  • I have already done that. Also will it make a difference if the page is a child page. And because the div is a server control the id gets changed – apporv Nov 18 '13 at 06:58
  • Ok will reply after i try at home. – Nuru Salihu Nov 18 '13 at 07:17
  • Did you try removing the
    as i suggested earlier?
    – Nuru Salihu Nov 18 '13 at 07:19
0

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....

Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
  • If you see, I am storing the value in another string `in_html` before clearing the `str_build`, so that I do not lose the values at the runtime. – apporv Nov 18 '13 at 07:41
0

I got my answer. Please check the code.

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

apporv
  • 1
  • 4