按钮单击时访问转发器行数据

时间:2017-06-26 10:08:56

标签: asp.net repeater

我的网页上有一个显示测试数据的转发器

<form id="frmQuestion">
            <div class="panel panel-primary">
              <div class="panel-body" style="max-height: 420;overflow-y: scroll;">
              <asp:ScriptManager ID="scriptmgr" runat="server"> </asp:ScriptManager>
                <asp:UpdatePanel ID="upd" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                <asp:Repeater ID="rptQuestions" runat="server">
                    <HeaderTemplate>
                        <table class="table" cellpadding="5px" cellspacing="10px" border="solid" id="tableQuestion">
                            <tr style="color:Black; background-color:Teal; border:solid;">
                                <th style="width:20px">No.</th>
                                <th style="width:100px">Question</th>
                                <th style="width:40px">QuestionType</th>
                                <th style="width:110px">Text Answer</th>
                                <th style="width:100px">Multiple Choice 1</th>
                                <th style="width:100px">Multiple Choice 2</th>
                                <th style="width:100px">Multiple Choice 3</th>
                                <th style="width:100px">Multiple Choice 4</th>
                                <th style="width:70px">True/False</th>
                                <th>Controls</th>
                            </tr>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr style="color:Black; background-color:Silver; border:solid;" id="trdata" class="trclass">

                            <td id="no"><asp:Label runat="server" ID="lblNo" Text='<%#Eval("Qno") %>' /></td>

                            <td id="que"><asp:Label runat="server" ID="lblQuestion" Text='<%#Eval("Question") %>'></asp:Label>
                            <asp:TextBox runat="server" ID="txtQuestion" Text='<%#Eval("Question") %>'  Visible="false"></asp:TextBox>
                            <asp:Label runat="server" ID="lblQid" Text='<%#Eval("Qid") %>' Visible="false"></asp:Label></td>

                            <td><asp:Label runat="server" ID="lblQtype" Text='<%#Eval("Qtype") %>' ></asp:Label>
                            <asp:DropDownList runat="server" ID="ddlQType" Visible="false"></asp:DropDownList>
                           <%-- <select class="dropdown" id="ddlQType" name="ddlQType" style="width:100px" runat="server">
                                        <option value="0">--Select--</option>
                                        <option value="1">Descriptive Answer</option>
                                        <option value="2">Multiple Choice </option>
                                        <option value="3">True/False answer</option></select>--%></td>

                            <td><asp:Label runat="server" ID="lblAnsDesc" Text='<%#Eval("description") %>' Width="100px" ></asp:Label>
                            <asp:TextBox runat="server" ID="txtAnsDesc" Text='<%#Eval("description") %>' Width="100px" Visible="false"></asp:TextBox></td>

                            <td><asp:Label runat="server" ID="lblch1" Text='<%#Eval("choice1") %>' Width="100px" ></asp:Label>
                            <asp:TextBox runat="server" ID="txtch1" Text='<%#Eval("choice1") %>' Width="100px" Visible="false"></asp:TextBox></td>

                            <td><asp:Label runat="server" ID="lblch2" Text='<%#Eval("choice2") %>' Width="100px" ></asp:Label>
                            <asp:TextBox runat="server" ID="txtch2" Text='<%#Eval("choice2") %>' Width="100px" Visible="false"></asp:TextBox></td>

                            <td><asp:Label runat="server" ID="lblch3" Text='<%#Eval("choice3") %>' Width="100px" ></asp:Label>
                            <asp:TextBox runat="server" ID="txtch3" Text='<%#Eval("choice3") %>' Width="100px" Visible="false"></asp:TextBox></td>

                            <td><asp:Label runat="server" ID="lblch4" Text='<%#Eval("choice4") %>' Width="100px" ></asp:Label>
                            <asp:TextBox runat="server" ID="txtch4" Text='<%#Eval("choice4") %>' Width="100px" Visible="false"></asp:TextBox>
                            <asp:Label runat="server" ID="lblCorrect" Text='<%#Eval("correct") %>' /></td>

                            <td><asp:Label runat="server" ID="lblisTrue" Text='<%# Eval("isTrue") %>' Width="70px" ></asp:Label>
                            <asp:CheckBox runat="server" ID="chkistrue" Checked='<%#(Eval("isTrue")=="true") %>' Width="70px" Visible="false"/></td>

                            <td> <span class="more"><asp:LinkButton ID="lnkEdit" Text="Edit" runat="server" data-toggle="modal" data-target="#myModal" OnClientClick="getdata();" CausesValidation="false" />
                                <asp:LinkButton ID="lnkUpdate" Text="Update" runat="server" Visible="false" OnClick="OnUpdate" CausesValidation="false" />
                                <asp:LinkButton ID="lnkCancel" Text="Cancel" runat="server" Visible="false" OnClick="OnCancel" CausesValidation="false" />
                                <asp:LinkButton ID="lnkDelete" Text="Delete" runat="server" OnClick="OnDelete" CausesValidation="false" OnClientClick="return confirm('Do you want to delete this row?');" />
                            </span></td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>
                </ContentTemplate>
                </asp:UpdatePanel>

            </div>
            </div>


             <!-- Modal example -->
  

添加问题&amp; nbsp&amp; nbsp&amp; nbsp

<!-- Modal -->
                                      ×             结果                                                                                                                                                                                                                                                                                                                                          - 选择 -                             描述性答案                             多项选择                             真/假答案                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                关                                                          
        </form>

现在我想获得转发器行数据,即btnUpdate_Click事件的问题详情。服务器代码是

 protected void BindRepeater(int tid)
    {
        try
        {

            cmd = new SqlCommand("SP_GetQuestions", connection);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Tid", tid);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();

            sda.Fill(dt);
            connection.Close();
            if (dt.Rows.Count > 0)
            {
                //adding qno to dt
                dt.Columns.Add("Qno",typeof(System.Int32));
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["Qno"] = i + 1;
                }
                rptQuestions.DataSource = dt;
                rptQuestions.DataBind();
            }
        }

        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

 protected Question getData()
    {
        Question question = new Question();
        question.qtype = Convert.ToInt32(lblQuestionType.Text);
        switch (question.qtype)
        {
            case 1:
                question.ansdesc = textareaAns.InnerText.ToString();
                break;
            case 2:
                question.ch1 = txtMulti1.Text;
                question.ch2 = txtMulti2.Text;
                question.ch3 = txtMulti3.Text;
                question.ch4 = txtMulti4.Text;
                question.correct = "";
                if (chkmulti1.Checked == true)
                    question.correct += "1";
                if (chkmulti2.Checked == true)
                    question.correct += "2";
                if (chkmulti3.Checked == true)
                    question.correct += "3";
                if (chkmulti4.Checked == true)
                    question.correct += "4";
                break;
            case 3:
                if (rbtrue.Checked == true)
                    question.isTrue = true;
                if (rbfalse.Checked == true)
                    question.isTrue = false;
                break;  
        }
        return question;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Question question = getData();
        question.qid = Convert.ToInt32(lblQuestid.Text);
        cmd = new SqlCommand("SP_UpdateQuestion", connection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@question", question.questDesc);
        cmd.Parameters.AddWithValue("@qid", question.qid);
        cmd.Parameters.AddWithValue("@ans", question.ansdesc);
        cmd.Parameters.AddWithValue("@ch1", question.ch1);
        cmd.Parameters.AddWithValue("@ch2", question.ch2);
        cmd.Parameters.AddWithValue("@ch3", question.ch3);
        cmd.Parameters.AddWithValue("@ch4", question.ch4);
        cmd.Parameters.AddWithValue("@correct", question.correct);
        cmd.Parameters.AddWithValue("@isTrue", question.isTrue);
        cmd.Parameters.AddWithValue("@qtype", question.qtype);
        try
        {
            connection.Open();
            cmd.ExecuteNonQuery();
            connection.Close();
            Response.Write("<script>alert('Question updated successfully!');</script>");
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

但编译器错误发生,因为ASP.addquestion_aspx'不包含'OnUpdate'的定义,并且没有扩展方法'OnUpdate'接受'ASP.addquestion_aspx'类型的第一个参数 我认为按钮是在转发器之外因此错误。如何访问转发器的数据以进行更新。请指导我。

1 个答案:

答案 0 :(得分:0)

实际上错误是由于我没有为转发器内的lnkUpdate按钮定义OnUpdate()方法。我刚刚删除了那个按钮,因为我不再需要它,现在工作正常。