在我的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
,以在页面加载时填充名为“ 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
选择按钮将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'列中的值),以便在另一个{{{ 1}}或类似的东西。当我尝试按列添加string ticketNumber = uxRowSelected.SelectedValue("Ticket Number");
等时,我得到了所有内容的两倍。有什么建议吗?
答案 0 :(得分:1)
我不确定这是否是您所需要的。但是您始终可以使用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();
}