You must Sign In to post a response.
  • Category: ASP.NET

    Add properties in existing html string using c#.net

    Hello.
    I have one situation where I have html string in session like below :-

    -----------------------------------------------------------------------
    <div id="DivQuestion"><div class="imgQuestion" id="divTextBx" style="text-align:right; position:relative; width: 666px;"><img src=""><div style="left:204px;position:absolute;width:300px;top:96px;margin-top:-23px;text-align:left;line-height: 23px;" id="draggable296-10"><span id="lbl296-10" style="text-align:left;width:30px; height:10px;font-size: 22px;font-weight:bold;color:#000000;">Select Prime numbers ?</span></div><div style="left:257px;position:absolute;width:0px;top:130px;margin-top:-23px;text-align:left;line-height: 23px;" id="draggable297-10"><div style="z-index:1000;position: absolute;"><div class="squaredTwo" style="float:left;margin-right:10px;"><input id="297_10" name="chkLov" value="" type="checkbox"><label for="squaredTwo"></label><span style="font-size: 22px;">20</span></div></div></div><div style="left:257px;position:absolute;width:0px;top:160px;margin-top:-23px;text-align:left;line-height: 23px;" id="draggable298-10"><div style="z-index:1000;position: absolute;"><div class="squaredTwo" style="float:left;margin-right:10px;"><input id="298_10" name="chkLov" value="" type="checkbox"><label for="squaredTwo"></label><span style="font-size: 22px;">17</span></div></div></div><div style="left:256px;position:absolute;width:0px;top:192px;margin-top:-23px;text-align:left;line-height: 23px;" id="draggable299-10"><div style="z-index:1000;position: absolute;"><div class="squaredTwo" style="float:left;margin-right:10px;"><input id="299_10" name="chkLov" value="" type="checkbox"><label for="squaredTwo"></label><span style="font-size: 22px;">19</span></div></div></div></div></div>
    -----------------------------------------------------------------------

    In this, I have created check boxes run time and pass it to view for rendering.
    Now, I want to add selected / checked propery in existing html string in c# depending of parameter / conditions.
    Its like, I have given question to student with options of check box.
    Once student submit the answer, I want to store answer given by Student. So, I want to add above properties while saving result into database.
  • #769496
    you can take variable and then you can apply your condition please look below code of your but modified

    string mystr = "<div id=\"DivQuestion\"><div class=\"imgQuestion\" id=\"divTextBx\" style=\"text-align:right; position:relative; width: 666px;\">"+
    "<img src=\"\"><div style=\"left:204px;position:absolute;width:300px;top:96px;margin-top:-23px;text-align:left;line-height: 23px;\" id=\"draggable296-10\">"+
    "<span id=\"lbl296-10\" style=\"text-align:left;width:30px; height:10px;font-size: 22px;font-weight:bold;color:#000000;\">Select Prime numbers ?</span>"+
    "</div><div style=\"left:257px;position:absolute;width:0px;top:130px;margin-top:-23px;text-align:left;line-height: 23px;\" id=\"draggable297-10\">"+
    "<div style=\"z-index:1000;position: absolute;\"><div class=\"squaredTwo\" style=\"float:left;margin-right:10px;\">";
    if(your parameter/condition)
    {
    mystr +="<input id=\"297_10\" name=\"chkLov\" value=\"\" type=\"checkbox\" checked=\"checked\">";
    }
    else
    mystr +="<input id=\"297_10\" name=\"chkLov\" value=\"\" type=\"checkbox\">";
    }
    mystr +="<label for=\"squaredTwo\"></label><span style=\"font-size: 22px;\">20</span></div></div></div>"+
    "<div style=\"left:257px;position:absolute;width:0px;top:160px;margin-top:-23px;text-align:left;line-height: 23px;\" id=\"draggable298-10\">"+
    "<div style=\"z-index:1000;position: absolute;\"><div class=\"squaredTwo\" style=\"float:left;margin-right:10px;\">";
    if(your parameter/condition)
    {
    mystr +="<input id=\"298_10\" name=\"chkLov\" value=\"\" type=\"checkbox\" checked=\"checked\">";
    }
    else
    {
    mystr +="<input id=\"298_10\" name=\"chkLov\" value=\"\" type=\"checkbox\">";
    }
    mystr +="<label for=\"squaredTwo\"></label><span style=\"font-size: 22px;\">17</span></div></div></div>"+
    "<div style=\"left:256px;position:absolute;width:0px;top:192px;margin-top:-23px;text-align:left;line-height: 23px;\" id=\"draggable299-10\">"+
    "<div style=\"z-index:1000;position: absolute;\"><div class=\"squaredTwo\" style=\"float:left;margin-right:10px;\">";
    if(your parameter/condition)
    {
    mystr +="<input id=\"299_10\" name=\"chkLov\" value=\"\" type=\"checkbox\" checked=\"checked\">";
    }
    else
    {
    mystr += "<input id=\"299_10\" name=\"chkLov\" value=\"\" type=\"checkbox\">";
    }
    mystr +="<label for=\"squaredTwo\"></label><span style=\"font-size: 22px;\">19</span></div></div></div></div></div>";

  • #769664
    i think instead of manipulating entire string every time you use HtmlDocument and load the HTML string into Object
    so, with the object you traverse to the check box and based on condition you change the attribute value or add new attribute.

    HtmlDocument html= new HtmlDocument();
    html.LoadHTML(strHtml);
    var root = html.DocumentNode;
    var p = root.Descendants();


    and using LINQ you can get the checkBox node and update back.

    Thanks!
    B.Ramana Reddy


  • Sign In to post your comments