0

我正在使用 asp.net c# 开发一个 Web 应用程序。我的项目是从 g 邮件下载邮件并在网格视图中显示。我已经下载了邮件并在网格视图中显示。在我的网站中,所有控件都只使用键盘键选择。我需要从网格视图中选择每一行而不鼠标单击只需在文本框中输入行索引,然后重定向到另一个页面。我在另一个网格视图中尝试了代码,它的工作 原理是如何通过在文本框中输入索引来选择网格视图行, 但是当我在我的应用中应用相同的代码时项目它不起作用

这是我的代码

收件箱.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>

收件箱.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));
        }
    }
4

2 回答 2

1

在 inbox.aspx.cs Page_Load 中,将 DtatTable 保存在 Session 中:

    ... ... ...
    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
        }
    }
}
于 2013-08-29T23:39:03.340 回答
1

你需要给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">
于 2013-08-30T10:32:57.287 回答