Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a method in a Button. Now I want to use this metod at Page Load.
So How it is possible?
Mwthod name= OnlyRead();

I am trying this ? It havesome error? so what is right?

Code--
C#
protected void Page_Load(object sender, EventArgs e)
    {
      Onlyread();
    {
        int rowIndex = 0;
        SqlConnection conn = new SqlConnection(connectionString);
        StringBuilder sb = new StringBuilder(string.Empty);

        try
        {
            int count = GridView2.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                TextBox box11 = (TextBox)GridView2.Rows[rowIndex].Cells[0].FindControl("TextBox11");
                TextBox box13 = (TextBox)GridView2.Rows[rowIndex].Cells[1].FindControl("TextBox13");
 

                rowIndex++;
                box11.ReadOnly = true;
                box13.ReadOnly = true;
               


            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }
           }
}
protected void BtnSave_Click(object sender, EventArgs e)
  {
OnlyRead();
}
Posted
Updated 23-Dec-11 19:05pm
v2

C#
protected void Page_Load(object sender, EventArgs e)
{
 if (Page.IsPostBack)
  {
     OnlyRead();
  }
}
private void Onlyread()
{
        int rowIndex = 0;
        SqlConnection conn = new SqlConnection(connectionString);
        StringBuilder sb = new StringBuilder(string.Empty);
 
        try
        {
            int count = GridView2.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                TextBox box11 = (TextBox)GridView2.Rows[rowIndex].Cells[0].FindControl("TextBox11");
                TextBox box13 = (TextBox)GridView2.Rows[rowIndex].Cells[1].FindControl("TextBox13");
 
 
                rowIndex++;
                box11.ReadOnly = true;
                box13.ReadOnly = true;
               
 

            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Error:";
            msg += ex.Message;
            throw new Exception(msg);
 
        }
           }
}
protected void BtnSave_Click(object sender, EventArgs e)
  {
OnlyRead();
}
 
Share this answer
 
Comments
[no name] 24-Dec-11 1:19am    
Good my 4
Monjurul Habib 24-Dec-11 1:22am    
thank you
C#
protected void Page_Load(object sender, EventArgs e)
{
 if (Page.IsPostBack)
  {
     OnlyRead();
  }
}

and write your onlyread() method and just call it from btnclick.
protected void BtnSave_Click(object sender, EventArgs e)
  {
    OnlyRead();
  }
 
Share this answer
 
v2
Just write :

C#
protected void Page_Load(object sender, EventArgs e)
{
   BtnSave_Click(null,null);
}


Note: But dear I can expect like this question if you are using C#. Learn more about C#
 
Share this answer
 
Comments
[no name] 24-Dec-11 1:19am    
pls look at whole code
Parwej Ahamad 24-Dec-11 1:28am    
I have seen the code what you want me to show?
Parwej Ahamad 24-Dec-11 1:30am    
As per my understanding you want to call OnlyRead method from Button click event and Page load event. so can achieve two way either direct call only read method on page load event or call button click event.
Hi friend , you can call OnlyRead() method on pageload.
 
Share this answer
 

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