我无法在onclick Button中使用的page_load中访问Button b1 = sender作为Button。我想在asp.net c#中使用page_load上的onclick按钮b1。注意:按钮用于gridview。请帮帮我。我将在下面分享我的代码。
这是aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class phoneLostData : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button b1 = sender as Button;
try
{
string dbAddress = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\mca\sem5\sem5Project\App_Data\Database.mdf;Integrated Security=True;User Instance=True; MultipleActiveResultSets=True;";
SqlConnection myCon = new SqlConnection(dbAddress);
myCon.Open();
string q1 = "select complainerName,status from lostPhnFir";
SqlCommand myCmd = new SqlCommand(q1, myCon);
SqlDataReader readIt = myCmd.ExecuteReader();
while (readIt.Read())
{
// Response.Write("alert('hi')");
if (readIt["complainerName"].Equals("abc") && readIt["status"].Equals("Checked"))
{
b1.Text = "Checked";
b1.Enabled = false;
}
}
readIt.Close();
myCon.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
public void Button1(object sender, EventArgs e)
{
Button b1 = sender as Button; // I want to use this b1 to page_load
string dbAddress = @"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\mca\sem5\sem5Project\App_Data\Database.mdf;Integrated Security=True;User Instance=True; MultipleActiveResultSets=True;";
SqlConnection myCon = new SqlConnection(dbAddress);
myCon.Open();
string q1 = "select complainerName from lostPhnFir";
int flag = 0;
SqlCommand myCmd = new SqlCommand(q1, myCon);
SqlDataReader readIt = myCmd.ExecuteReader();
if (b1.ID == "btnFIRLodge")
{
b1.Text = "Checked";
b1.Enabled = false;
flag = 1;
}
if (flag == 1)
{
string status1 = "Checked";
string s1 = "abc";
string sql = "update lostPhnFir set status='" + status1 + "' where complainerName='" + s1 + "'";
SqlCommand myCmd2 = new SqlCommand(sql, myCon);
myCmd2.ExecuteNonQuery();
}
}
}
这是aspx。
<asp:TemplateField HeaderText="FIR Lodged" ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnFIRLodge" runat="server"
Text="Send Message" onclick="Button1"></asp:Button>
</ItemTemplate>
</asp:TemplateField>