Click here to Skip to main content
15,867,851 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have created dynamically htmltable in codebind as

source page
<div id="bindHTMLhere"  runat="server" />


and in codebehind
 tblString.Append(" <tr>");
  for (int i = 0; i < 3; i++)
                      {
tblString.Append("<td >");
tblString.Append("   <select value='" + dataPO.Tables[2].Rows[i]["add_less"].ToString().TrimStart().TrimEnd() + "' id='ddlAddLess_Row1' TabIndex='" + (++ddlindex) + "' runat='server' style='width:10%;height:23px;'  onkeyup='toUnicode(this,event)' class='Assettextbox'></select>");
 tblString.Append("    </td>");
                     }
  tblString.Append(" </tr>");


bindHTMLhere.InnerHtml = tblString.ToString();


I want to bind this dropdown (select) with the database and also want to set its selected value .

Notes :1) I dont want to use jquery for creating dynamic html table.
2) I want to create my html table and select in codebind.


Please help
Posted
Updated 10-Nov-14 1:10am
v2
Comments
Sinisa Hajnal 10-Nov-14 7:45am    
Why!? Even doing server side, you'd probably be better using HTML objects rather then plain string. If you want to use string, use String.Format so you have 'clean' HTML with placeholders rather then bunch of + "' in variuos combinations...

There is AppendFormat method that works for string builder. Also, There is Trim function, you don't have to call TrimStart and TrimeEnd.

Read this:
String.Format(<select value='{0}' id='ddlAddLess_Row1' TabIndex='{1}' runat="server" style='width:10%;height:23px;' ></select>", dataPO.Tables[2].Rows[i]["add_less"].ToString().Trim, (++ddlindex) )

1 solution

Hi,


you can add Option List into HTML String as
C#
string ChoiceHtml = "";

for(var i=0;i<dataPO.Tables[2].Rows.Count;i++)
{
  var valField = dataPO.Tables[2].Rows[i]["ValueField"];
  var txtField = dataPO.Tables[2].Rows[i]["TextField"];
  if(dataPO.Tables[2].Rows[i]["add_less"]==1)
  {
    ChoiceHtml += "<option selected = 'true' value="+ valField +"> " + txtField   +" </option>";
  } 
  else
  {
    {
    ChoiceHtml += "<option value="+ valField +"> " + txtField   +"   </option>";
  } 
  }
  
}


at the time of re select exchange "Selected" property for option , and re assign html string as
C#
tblString.Append("   <select value='" + dataPO.Tables[2].Rows[i]["add_less"].ToString().TrimStart().TrimEnd() + "' id='ddlAddLess_Row1' TabIndex='" + (++ddlindex) + "' runat="'server'" style='width:10%;height:23px;'  onkeyup='toUnicode(this,event)' class='Assettextbox'>"+ ChoiceHtml   +"</select>");
 tblString.Append("    ");

 bindHTMLhere.InnerHtml = tblString.ToString();





Thanks
 
Share this answer
 
v2
Comments
aassaahh 10-Nov-14 8:32am    
how do i know which option to get selected ,since selected value will be coming from database. dataPO.Tables[2].Rows[i]["add_less"]
rmksiva 10-Nov-14 8:53am    
you need to build "ChioceHtml" at runtime and update whole html string , Because Postback clear all dynamic binded html elements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900