how to select grid view row by entering index in a

2019-08-26 08:16发布

问题:

i am working on a web application in asp.net c#. My project is that download mails from g mail and show it in a grid view.i have downloaded mails and displayed in grid view.in my website all the controls are selected only using keyboard keys.i need to select each row from grid view without mouse click by simply enter the row index in a text box then redirect to another page.i tried the code in another grid view and its worked how select grid view row by entering index in a text box but when i applied the same code in my project its not working

Here is my code

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));
        }
    }

回答1:

you need to give DataKeyNames="MessageNumber" as below

<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">


回答2:

In inbox.aspx.cs Page_Load, save the DtatTable in Session:

    ... ... ...
    Session["Messages"] = dtMessages;
    gvinbox.DataSource = dtMessages;
    gvinbox.DataBind();
}

In Showmessage.aspx.cs Page_Load get the table row:

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);
            }

        }
    }
}   

Hope it makes sense!

Edit: To avoid Index out of range error we need fix the btnread_Click method like below:

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
        }
    }
}