在我的ASP.Net应用程序中,我使用存储过程从我的SQL Server表中填充“网格视图”,该存储过程根据存储的用户名从表中选择数据。我首先在一个名为“DataAccessManager.cs”的单独C#文件中管理存储过程和数据:
public class DataAccessManager
{
private string _connString;
private Dictionary<string, string> _questions;
private Dictionary<string, string> _answers;
public DataAccessManager()
{
_connString = ConfigurationManager.ConnectionStrings["DevConnectionString"].ToString();
_questions = new Dictionary<string, string>();
_answers = new Dictionary<string, string>();
}
//
public DataTable GetTicketByOwnership(string UserName)
{
DataTable ticketDT = null;
SqlConnection conn = null;
try
{
conn = new SqlConnection(_connString);
conn.Open();
SqlCommand cmd = new SqlCommand("dbo.a_GetTicketByOwnership", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds);
ticketDT = ds.Tables[0];
}
finally
{
if (conn != null)
{
conn.Close();
}
}
return ticketDT;
}
....
}
然后我调用GetTicketByOwnership(string UserName)
返回数据表ticketDT
,在Page Load上填充名为'uxTicketGridView'的Gridview。
public partial class Summary : Page
{
DataAccessManager _dtMgr;
protected int _widestDataLength;
DataTable _mainDT;
protected void Page_Load(object sender, EventArgs e)
{
_widestDataLength = 0;
_dtMgr = new DataAccessManager();
Session["UserNameSession"] = "jbrown";
Session["isUserAuthenticated"] = true;
string staffName = _dtMgr.GetStaffNameByUser(Session["UserNameSession"].ToString());
if (staffName == string.Empty)
{
//error
}
else
{
Session["StaffName"] = staffName;
uxStaffNameTextbox.Text = "Hello " + staffName;
_mainDT = _dtMgr.GetTicketByOwnership(Session["UserNameSession"].ToString());
....
if (!Page.IsPostBack)
{
uxTktGridView.DataSource = _mainDT;
uxTktGridView.DataBind();
....
回到HTML中,asp:GridView
is使用CommandField
选择按钮添加到DOM:
<asp:GridView ID="uxTktGridView" runat="server" CssClass="GridView" BorderStyle="Solid" onRowDataBound="uxTktGridView_RowDataBound" OnSorting="uxTktGridView_Sorting" BackColor="White" BorderColor="#D6D2D2" BorderWidth="1px" CellPadding="3" SelectedIndex="-1" AllowSorting="True" Font-Size="Small" Width="100%" Visible="True" EnableModelValidation="True" style=" margin-top: 10px; margin-bottom: 10px;">
<Columns>
<asp:CommandField ShowSelectButton="True" SelectText="Details" ButtonType="Button" HeaderText="Select" />
</Columns>
....
</asp:GridView>
我现在的问题是'如何在不使用gridview
的情况下添加功能来选择asp:BoundFields
中的行?那可能吗?我可以在单击按钮时选择行,但我需要的是从该行中提取一条信息(比如“Ticket Number”列中的值)以在另一个存储过程中使用,如string ticketNumber = uxRowSelected.SelectedValue("Ticket Number");
或相似的东西。当我尝试逐列添加<asp:BoundField DataField="TicketNumber" HeaderText="Ticket Number" />
等时,我最终得到了所有内容的双打。有什么建议?
我不确定这是否是你需要的。但你总是可以使用DataKeyNames
。您可以在GridView中定义它们
<asp:GridView ID="GridView1" runat="server" DataKeyNames="id, name">
然后,您始终可以使用正确的行号和DataKey访问这些值。
protected void Button1_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[i].Values[0]);
string name = GridView1.DataKeys[i].Values[1].ToString();
}