简体   繁体   English

更改 GridView 分页索引时是否可以防止回发?

[英]Is it possible to prevent postback when changing the GridView Paging Index?

I am trying to create my own online quiz website using ASP C#.我正在尝试使用 ASP C# 创建我自己的在线测验网站。

I tried to bind the question from database into gridview and set the page size of the gridview to 1. And then I tried to pull the answers option (4 option) into radioButton .我想这个问题从数据库装订成gridview ,并设置页面大小gridview为1。然后我试图把答案选项(4选件)为radioButton

But when I tried to move from the 1 page of gridview to another page, the checked value lost.但是当我试图从gridview的第一页移动到另一页时,检查的值丢失了。 I know thats because of when we changed the gridview page, it will do a postback .我知道那是因为当我们更改gridview页面时,它会进行postback

My Questions are:我的问题是:

  1. Why I can't access my RadioButton from CodeBehind?为什么我无法从 CodeBehind 访问我的RadioButton Is it because of inside the Updatepanel ?是因为在 Updatepanel 里面吗?
  2. Is it possible if we want to do a page index change in gridview without postback ?如果我们想在不postback情况下在 gridview 中进行页面索引更改,是否有可能? Or,或者,
  3. is there another option to make my RadioButton checked value not lost when changing to the next page (next question) ?是否有另一个选项可以让我的RadioButton选中的值在切换到下一页时不会丢失(下一个问题)?

here are my code :这是我的代码:

** quiz.aspx : ** **测验.aspx: **

<asp:UpdatePanel ID="upd1" runat="server">
    <ContentTemplate>
        <asp:GridView ID="grdquestions" runat="server" EnableViewState="true" EnableSortingAndPagingCallbacks="false" OnPageIndexChanging="grdquestions_PageIndexChanging" AutoGenerateColumns="false" DataKeyNames="QuestionId" Width="100%" AllowPaging="True" AllowCustomPaging="False" PageSize="1" ViewStateMode="Enabled">
            <Columns>
                <asp:TemplateField HeaderText="DOJO eQuiz">
                    <ItemTemplate>
                        <table class="tableclass" id='<%#Eval("QuestionId") %>'>
                            <tr>
                                <td><b>Question : <%#Eval("Question") %></b>
                                    <asp:HiddenField ID="hdnquestionId" runat="server" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <table>

                                        <tr style="margin-top: 10px;">

                                            <td>
                                                <table class="tblOptions">
                                                    <tr>
                                                        <td>
                                                            <asp:RadioButton ID="rdOption1" AutoPostBack="true" runat="server" Text='<%#Eval("Option1") %>' GroupName="chcbox" />

                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:RadioButton ID="rdOption2" runat="server" AutoPostBack="true" Text='<%#Eval("Option2") %>' GroupName="chcbox" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:RadioButton ID="rdOption3" runat="server" AutoPostBack="true" Text='<%#Eval("Option3") %>' GroupName="chcbox" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:RadioButton ID="rdOption4" runat="server" Text='<%#Eval("Option4") %>' GroupName="chcbox" />
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td>
                                                            <asp:Label ID="lbquestionstatus" runat="server"></asp:Label>
                                                        </td>
                                                    </tr>

                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>

                        </table>

                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle HorizontalAlign="Center" CssClass="GridPager" />
        </asp:GridView>
    </ContentTemplate>
</asp:UpdatePanel>

** quiz.aspx.cs ** **测验.aspx.cs **

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("Login.aspx");

        }
        else
        {
            if (!Page.IsPostBack)
            {
                BindGrid();

            }
            else
            {
            }
        } 
    }


 public void BindGrid()
    {
        var sess = Session["username"].ToString();
        string a = sess.Substring(sess.Length - 1);

        SqlDataAdapter adp = new SqlDataAdapter("select * from dojoquiz order by " + a + "", ConfigurationManager.ConnectionStrings["dashboardConnectionString"].ToString());
        adp.Fill(dt);

        grdquestions.DataSource = dt;
        grdquestions.DataBind();
    }

protected void grdquestions_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButtonList rdlstOptions = (RadioButtonList)e.Row.FindControl("rdlstOptions");
            HiddenField hdnquestionId = (HiddenField)e.Row.FindControl("hdnquestionId");
            if (rdlstOptions != null && hdnquestionId != null)
            {
                DataRow[] result = dt.Select("questionid=" + (Convert.ToInt32(hdnquestionId.Value)));
                DataView view = new DataView();
                view.Table = dt;
                view.RowFilter = "questionid=" + (Convert.ToInt32(hdnquestionId.Value));
                if (view.Table.Rows.Count > 0)
                {
                    DataTable dt1 = new DataTable();
                    dt1 = view.ToTable();
                }
            }
        }
    }

    protected void grdquestions_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdquestions.PageIndex = e.NewPageIndex;
        this.BindGrid();
    }

Thank you for any help and suggestions.感谢您的任何帮助和建议。

If the sql query is not change, then don't do DataBind() on post back, but you still need to give him the data source to get the data.如果sql查询没有变化,那么在post back时不要做DataBind() ,但你仍然需要给他数据源来获取数据。 So your code must be as:所以你的代码必须是:

protected void Page_Load(object sender, EventArgs e)
{
    BindGrid();
}

public void BindGrid()
{
    var sess = Session["username"].ToString();
    string a = sess.Substring(sess.Length - 1);

    SqlDataAdapter adp = new SqlDataAdapter("select * from dojoquiz order by " + a + "", ConfigurationManager.ConnectionStrings["dashboardConnectionString"].ToString());
    adp.Fill(dt);

    grdquestions.DataSource = dt;

    if (!Page.IsPostBack)
        grdquestions.DataBind();
}

Its not possible with out post back to change page or do thinks on GridView, but if you make it correct this is not an issue.如果不回帖更改页面或在 GridView 上进行思考,这是不可能的,但是如果您将其改正,这不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM