我正在使用asp.net c#中的web应用程序。我的项目是从g mail下载邮件并在网格视图中显示。我已下载邮件并以网格视图显示。在我的网站中,所有控件仅使用键盘键选择。我需要从网格视图中选择每一行而不鼠标单击只需在文本框中输入行索引然后重定向到另一个页面。我尝试了另一个网格视图中的代码,并且其工作正常 how select grid view row by entering index in a text box 但是当我在我的项目中应用相同的代码时,它无法正常工作
这是我的代码
inbox.aspx
<asp:Label ID="lblusername" runat="server" Font-Bold="True" Font-Size="X-Large"
ForeColor="#A2979E"></asp:Label>
<br />
<asp:TextBox runat="server" ID="txtindex"></asp:TextBox>
<asp:Button runat="server" Text="Read Mail" ID="btnread"
onclick="btnread_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<asp:GridView ID="gvinbox" runat="server" AutoGenerateColumns="false"
ShowFooter="false" Height="103px" Width="795px" BorderStyle="Solid"
Font-Bold="True" Font-Names="Century Gothic" CellPadding="7"
style="margin-right: 0px">
<Columns>
<asp:BoundField HeaderText="FROM" DataField="From" />
<asp:HyperLinkField HeaderText="SUBJECT" DataNavigateUrlFields="MessageNumber" DataNavigateUrlFormatString="~/Showmessage.aspx?MessageNumber={0}" Target="_blank" DataTextField="Subject" />
<asp:BoundField HeaderText="DATE" DataField="DateSent" />
</Columns>
</asp:GridView>
inbox.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Pop3Client pop3Client;
if (Session["Pop3Client"] == null)
{
pop3Client = new Pop3Client();
pop3Client.Connect("pop.gmail.com", int.Parse("995"), true);
pop3Client.Authenticate("abc@gmail.com", "123456");
Session["Pop3Client"] = pop3Client;
}
else
{
pop3Client = (Pop3Client)Session["Pop3Client"];
}
int count = pop3Client.GetMessageCount();
DataTable dtMessages = new DataTable();
dtMessages.Columns.Add("MessageNumber");
dtMessages.Columns.Add("From");
dtMessages.Columns.Add("Subject");
dtMessages.Columns.Add("DateSent");
dtMessages.Columns.Add("Attachment");
int counter = 0;
for (int i = count; i >= 1; i--)
{
Message message = pop3Client.GetMessage(i);
dtMessages.Rows.Add();
dtMessages.Rows[dtMessages.Rows.Count - 1]["MessageNumber"] = i;
dtMessages.Rows[dtMessages.Rows.Count - 1]["From"] = message.Headers.From.Address;
dtMessages.Rows[dtMessages.Rows.Count - 1]["Subject"] = message.Headers.Subject;
dtMessages.Rows[dtMessages.Rows.Count - 1]["DateSent"] = message.Headers.DateSent.ToLocalTime();
counter++;
if (counter > 10)
{
break;
}
}
gvinbox.DataSource = dtMessages;
gvinbox.DataBind();
}
protected void btnread_Click(object sender, EventArgs e)
{
int index, aid;
if (int.TryParse(txtindex.Text, out index) &&
int.TryParse((gvinbox.DataKeys[index].Value.ToString()), out aid))
{
Response.Redirect(string.Format("Showmessage.aspx?MessageNumber={0}", aid));
}
}
答案 0 :(得分:0)
在inbox.aspx.cs Page_Load中,在会话中保存DtatTable:
... ... ...
Session["Messages"] = dtMessages;
gvinbox.DataSource = dtMessages;
gvinbox.DataBind();
}
在Showmessage.aspx.cs中,Page_Load获取表格行:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int messageNumber = 0;
DataTable dtMessages = (DataTable)Session["Messages"];
if (int.TryParse(Request.QueryString["MessageNumber"].ToString(), out messageNumber) && dtMessages != null)
{
var m = (from DataRow dr in dtMessages.Rows
where (string)dr["MessageNumber"] == messageNumber.ToString()
select dr).FirstOrDefault();
if (m != null)
{
string sOut = m["From"].ToString() + "<br />" +
m["Subject"].ToString() + "<br />" +
m["DateSent"].ToString();
Response.Write(sOut);
}
}
}
}
希望它有意义!
编辑:为避免索引超出范围错误,我们需要修复btnread_Click方法,如下所示:
protected void btnread_Click(object sender, EventArgs e)
{
int index, aid;
if (int.TryParse(txtindex.Text, out index))
{
// Index is 0 based, but we input row number from 1
// So we have to sutract 1 from input
index--;
if ((index >= 0) && (index <= gvinbox.DataKeys.Count) && (int.TryParse((gvinbox.DataKeys[index].Value.ToString()), out aid))
{
Response.Redirect(string.Format("Showmessage.aspx?MessageNumber={0}", aid));
}
else
{
// Input out of range
// Do whatever to display error
}
}
}
答案 1 :(得分:0)
您需要提供DataKeyNames="MessageNumber"
,如下所示
<asp:GridView ID="gvinbox" runat="server" AutoGenerateColumns="false"
ShowFooter="false" Height="103px" Width="795px" BorderStyle="Solid"
Font-Bold="True" Font-Names="Century Gothic" CellPadding="7"
style="margin-right: 0px" DataKeyNames="MessageNumber">