Mobile Content

Monday, February 28, 2011

To fire two event when button is click

protected void Button1_Click(object sender,EventArgs e)
{
     respose.write("<script type='javascript'">
response.write("window.open('default.aspx');");
response.write("</script>");
sqlconnection con=new sqlconnection("connection string.");
sqlcommand cmd=new sqlcommand("insert into sub value('a','b')",con);
con.open();
cmd.ExecuteNonQuery();
con.close();
}

it will fire two event at a time, it will popup the default.aspx page and also insert data into sub table...

Monday, February 21, 2011

Two way to refresh the page using javascript

1. Using link.
<a href="javascript:location.reload(true)">Refresh </a>
it will show link "Refresh" on page by clicking, it will refresh the page




2. Automatic refreshing  of page using javascript
<html>

<head>
<script type="text/JavaScript">
function timedRefresh(timeoutPeriod) {
    setTimeout("location.reload(true);",timeoutPeriod);
}
</script>
</head>
<body onload="JavaScript:timedRefresh(5000);">
</body>
</html>

it will refresh page every 5 sec when page is loaded

Sunday, February 20, 2011

Dynamically generate button or Textbox according to the data in database in asp.net

 For generating dynamic button and textbox according to data present in database
if(!IsPostBack)
       {string[] nm=new string[100];
            SqlConnection con = new SqlConnection("connection string....");
            SqlCommand cmd = new SqlCommand("sql query.....", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            int cnt = 0;
            int c=0;
            while (dr.Read())
            {
                cnt++;
                nm[c]=dr[0].ToString();
                c=c+1;
              }
            con.Close();
                
                for (int i = 0; i < cnt; i++)
                {
                    Button btn = new Button();                        TextBox txt = new TextBox();
                    btn.Text = nm[i];
                     btn.ID = i + "id";                                       txt.ID = i + "txt";
                     form1.Controls.Add(btn);                        txt.Width = 16;
                                                                                     txt.Height = 16;
                                                                                     txt.BorderStyle = BorderStyle.None;
                                                                                  //   txt.Enabled = false;
                                                                                        form1.Controls.Add(txt);
                }
                      
       }
it will generate the dynamic button and textbox according to data present in table

Refresh the gridview without refreshing the whole page using Ajax in asp.net

To refresh particular control we have to use <asp:UpdatePanel> with <asp:Timer> Ajax control.

Before taking this control we have to take <asp:ScriptManager> 
in aspx page
 <asp:ScriptManager ID="ScriptManager1" runat="server">
 </asp:ScriptManager>
  <asp:UpdatePanel ID="UpdatePanel2" runat="server">
  <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" Interval="5000">
        </asp:Timer>
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
       <Columns>
       <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
       <asp:BoundField DataField="Message" HeaderText="Message"
                SortExpression="Message" />
       </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
        SelectCommand="SELECT [Date], [Message] FROM [ChatDb]"></asp:SqlDataSource>
    </ContentTemplate>
    <Triggers>
   <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
    </Triggers>
    </asp:UpdatePanel>
in .cs page

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();
        }
    }
OR

 protected void Timer1_Tick(object sender, EventArgs e)
    {

            GridView1.DataSource = SqlDataSource1;
            GridView1.DataBind();

    }

Saturday, February 19, 2011

popup occur by clicking button control in asp.net

we can popup using javascript in .aspx file or aspx.cs file
in aspx file
<script language="javascript">
function pop(url)
{window.open("url");}
</script>
<asp:Button id="Button1" runat="server" Text="popup" onclick="pop('default.aspx')"/>// default.aspx page will pop up when we click popup button.

or we can also write        onclick="javascript:window.open('default.aspx')"/>;

In .cs file
we can write
Button1.attributes.add("onclick","window.open('default.aspx')");

To refresh the page : html doc

<meta http-eqiv="Refresh" content="5" id="refresh" runat="server"/>

content=contain the second the page will refresh
we can refresh the page in .cs file by using id of <meta>
eg:  refresh.content=5;