views:

66

answers:

2

I have a repeater that is supposed to create div tags with their onclick functions from databind. Which is like

onclick='<%# "functionname('" + Eval("somestring") +"');" %>'

but the single quotes ' after the js func ( and before ) , they become something like ?,= ..

I don't understand what's happening. .. the full code is below

<div id="dvPager">
    <asp:Repeater ID="pagerSorular" runat="server">
    <ItemTemplate>
     <div class='<%# "pageritem pagertext" + ((this.PageNumber == int.Parse(Container.DataItem.ToString())) ? " pagerselected" : "") %>'
          onclick='<%# "gotopage('" + this.PageName + "', " + Container.DataItem + ");" %>'>
      <%# Container.DataItem %>
     </div>
    </ItemTemplate>
    </asp:Repeater>
</div>

and codebehind is:

public int PageNumber 
{
    get
    {
       string strPgNum = Request.QueryString["pg"] as String;
       if (!String.IsNullOrEmpty(strPgNum))
       {
           int pgnum;
           if (int.TryParse(strPgNum, out pgnum))
           {
               if (pgnum <= 0)
               {
                   return pgnum;
               }
               else
               {
                   return 1;
               }
           }
           else
           {
               return 1;
           }
       }
       else
       {
           return 1;
       }
    }

}
public string PageName {
    get
    {
        string url = Request.Url.ToString();
        string[] parts = url.Split(new char[]{'/'});
        return parts[parts.Length - 1];
    }
}
public void Doldur(List<SoruGridView> sorulistesi)
{
    gridSorular.DataSource = sorulistesi;
    gridSorular.DataBind();
    ///
    PagerYap(sorulistesi);
}

protected void PagerYap(List<SoruGridView> sorulistesi)
{
    List<int> numpgs = new List<int>(); int count = 1;
    foreach (SoruGridView sgv in sorulistesi)
    {
        numpgs.Add(count);
        count++;
    }
    pagerSorular.DataSource = numpgs.ToArray();
    pagerSorular.DataBind();

}
+1  A: 

You will have to html-encode your single quotes after "functionname(", as in this SO question:

http://stackoverflow.com/questions/1873/triple-quotes-how-do-i-delimit-a-databound-javascript-string-parameter-in-asp-n

Looks like you can simplify your code a bit as a result, too:

onclick='functionname(&#39;<%# Eval("somestring")%>&#39);'

Looks much more nasty, though, sorry that can't be helped.

mattdekrey
that was actually lot simplified for the sake of the question ... thanks for the ampersends tough, saved me a google search for that :)
emre
You cann tell me what did you type into the search box to find this question that has been asked before tough .. that will really help in the future
emre
I recognized that it wasn't escaping your quotes properly (I'm surprised ASP.Net didn't give you an invalid HTML error or something, actually), so entered "asp.net databind escape quotes" into google. Happy coding!
mattdekrey
A: 

Thanks a lot for the answer. Since this is a duplicate of http://stackoverflow.com/questions/1873/triple-quotes-how-do-i-delimit-a-databound-javascript-string-parameter-in-asp-n

please delete this question. Thx!

emre