如何在按钮上单击页面加载时调用方法 [英] How to Call a Method on Page Load at a Button Click in Asp.net
本文介绍了如何在按钮上单击页面加载时调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Button中有一个方法.现在,我想在Page Load中使用此方法.
那怎么可能呢?
Mwthod name = OnlyRead();
我正在尝试吗?有一些错误吗?那是正确的吗?
代码-
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--
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();
}
推荐答案
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();
}
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();
}
只需编写:
Just write :
protected void Page_Load(object sender, EventArgs e)
{
BtnSave_Click(null,null);
}
注意:但是亲爱的,如果您使用的是C#,我可以期望像这样的问题.了解有关C#的更多信息
Note: But dear I can expect like this question if you are using C#. Learn more about C#
这篇关于如何在按钮上单击页面加载时调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文