Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
protected void Page_Load(object sender, EventArgs e)
    {
          if (!IsPostBack) {
              string connectionstring = "Data source=xx; Initial Catalog=DB; Integrated Security=true";
              System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(connectionstring);
              conn.Open();
              System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand("select top 50 MCUR_Currency_Code,  MCUR_Description, MCUR_Short_Description from sqlmas.gen_m_currencies", conn);
              System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(comm);
              System.Data.DataSet ds = new System.Data.DataSet();
              da.Fill(ds);
              Repeater1.DataSource = ds;
              Repeater1.DataBind();
          }
    }



protected void R1_ItemDataBound(Object Sender, RepeaterItemEventArgs e) 
       {
          if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
              this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Warning", "abc('test')", true);
          }
       }    



<script language="javascript" type= "text/javascript">
function abc(t){
alert('dfsd')
}
</script>

<asp:Repeater id="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
          <HeaderTemplate>
             <table border="1">
                <tr>
                   <td><b>Product</b></td>
                   <td><b>Consumer Rating</b></td>
                </tr>
          </HeaderTemplate>

          <ItemTemplate>
             <tr>
                <td> <asp:Label ID="Label1" Text='<%# DataBinder.Eval(Container.DataItem, "MCUR_Currency_Code") %>' Runat="server"/> </td>
                <td> <asp:Label id="RatingLabel" Text='<%# DataBinder.Eval(Container.DataItem, "MCUR_Description") %>' Runat="server"/> </td>
             </tr>
          </ItemTemplate>

          <FooterTemplate>
             </table>
          </FooterTemplate>

       </asp:Repeater>


What I have tried:

<asp:Repeater id="Repeater1" OnItemDataBound="R1_ItemDataBound" runat="server">
Posted
Updated 21-Sep-16 23:38pm

1 solution

One thing you have to understand is that you're not calling javascript from your .net code. Your .net code runs and creates html to send to the browser, once all your .net code has run the browser gets the results and executes them and at that point you can run javascript. What RegisterStartupScript is doing is adding js to the output in a location that means it will execute when the page finally loads in the browser. The "Warning" parameter is the key. What you're doing is labelling your "abc('test')" as "Warning" and keys have to be unique so when you try and register that js for a second or third time then .net sees you've already added code with the key of "Warning" so it simply ignores your subsequent request to add.

So what you need to do is use a unique key each time you register the script. An easy way of doing this is to add the item index to the key;

C#
this.Page.ClientScript.RegisterStartupScript(this.GetType(), string.Concat("Warning", e.Item.ItemIndex.ToString()), "abc('test')", true);


That will generate keys like "Warning0", "Warning1" etc.
 
Share this answer
 
Comments
Sathish km 22-Sep-16 6:24am    
Thanks. but This doesn't give any alert
I have 10 records to bin into repeater, so it will hit R1_ItemDataBound 10 times, so i need to trigger javascript function for 10 times.
F-ES Sitecore 22-Sep-16 6:53am    
I can't see how it will stop all the alerts, all you're doing is changing the key.
Sathish km 22-Sep-16 7:07am    
Got it..

Page.ClientScript.RegisterStartupScript(this.GetType(), string.Format("CallMyFunction{0}", incre), "timer(" + incre + ");", true);

have to pass uniqueid to call more then one time.

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