简体   繁体   English

ASP.NET JQuery 无法访问服务器和 HTML 控件

[英]ASP.NET JQuery unable to access Server and HTML Controls

I'm having problems with integrating JavaScript to ASP.NET.我在将 JavaScript 集成到 ASP.NET 时遇到问题。 I want to use jQuery or even Vanilla JavaScript to do some client side operations but I'm unable to access HTML and ASP.NET Server Controls.我想使用 jQuery 甚至 Vanilla JavaScript 来执行一些客户端操作,但我无法访问 HTML 和 Z9E8DD308438E1CE38A 服务器。 I have tried many solution, just like using <%= element.ClientID %> but none worked for me.我尝试了很多解决方案,就像使用<%= element.ClientID %>但没有一个对我有用。 I'm unable to identify my fault, please help me identifiying.我无法识别我的错误,请帮助我识别。

Alread Tried :已经尝试过

  1. RegisterClientScriptBlock
  2. Include JavaScript File Directly直接包含JavaScript文件
  3. $("#<% element.ClientID %>")
  4. document.getElementById("<%= element.ClientID %>") ; document.getElementById("<%= element.ClientID %>") ;
  5. Using HTML control instead Server & vice versa.使用 HTML 控件代替服务器,反之亦然。

new_quiz.aspx新测验.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/teacher_quizes/teacher_quizes.master" AutoEventWireup="true" CodeBehind="new_quiz.aspx.cs" Inherits="onlineQuiz_bsef17m35.teacher_quizes.new_quiz" %>
<asp:Content ID="Content1" ContentPlaceHolderID="quizes" runat="server">
  <div class="form p-2">
    <div class="form-group p-1">
      <asp:Label runat="server" Text="Quiz Title"></asp:Label><br />
      <small class="text-danger">The title of your quiz</small>
      <input runat="server" type="text" maxlength="64" id="title"
        class="form-control"/>
      <asp:RequiredFieldValidator runat="server" ControlToValidate="title"
        ErrorMessage="Title is required"></asp:RequiredFieldValidator>
    </div>

    <div class="form-group p-1">
      <asp:Label runat="server" Text="Quiz Description"></asp:Label><br />
      <small class="text-danger">Please describe this quiz</small>
      <textarea runat="server" id="description" class="form-control" rows="2"
        maxlength="128">
      </textarea>
      <asp:RequiredFieldValidator runat="server" ControlToValidate="description"
        ErrorMessage="Description is required" CssClass="text-warning text-muted">
      </asp:RequiredFieldValidator>
    </div>

    <div class="form-group p-1">
      <asp:Label runat="server" Text="Maximum Marks"></asp:Label><br />
      <small class="text-danger">The maximum achiveable marks for this quiz, between 1 and 1000</small>
      <input runat="server" type="number" min="1" max="100" id="maxMarks"
        class="form-control"/>
      <asp:RangeValidator MinimumValue="1" MaximumValue="1000"
        runat="server" ControlToValidate="maxMarks" CssClass="text-warning"
        ErrorMessage="Please choose a number between 1 and 100">
      </asp:RangeValidator>
    </div>

    <div class="form-group p-1">
      <asp:Label runat="server" Text="Passing Marks"></asp:Label><br />
      <small class="text-danger">The passing marks for this quiz, between 1 and 1000 and lesser than or equal to total marks</small>
      <input runat="server" type="number" min="1" max="100" id="passingMarks"
        class="form-control"/>
      <asp:RangeValidator MinimumValue="1" MaximumValue="1000"
        runat="server" ControlToValidate="passingMarks" CssClass="text-warning"
        ErrorMessage="Please choose a number between 1 and 100">
      </asp:RangeValidator>
    </div>

    <div class="form-group p-1 card mb-2">
      <div><b runat="server" id="questions">Quesions</b></div>
      <div class="form-group">
        <asp:Label runat="server" Text="Question"></asp:Label><br />
        <asp:TextBox runat="server" ID="question" CssClass="form-control"/>

        <asp:Label runat="server" Text="Type of Question"></asp:Label><br />
        <select runat="server" id="questionType" class="form-control"
          onchange="questionTypeChange()"></select>

        <div runat="server" id="questionOptions">
          <small>Please check checkbox in front of Options to denote an option as true</small>

          <div class="form-group">
            <asp:Label runat="server" Text="Option 1"></asp:Label><br />
            <input runat="server" type="text" maxlength="64" id="questionOption1"
              class="form-control" />
            <input runat="server" type="checkbox" id="questionOption1Validity" 
              class="input-group-append"/>
            <asp:RequiredFieldValidator runat="server" ControlToValidate="questionOption1"
              ErrorMessage="Option 1 is required!" CssClass="text-danger">
            </asp:RequiredFieldValidator>
          </div>

          <div class="form-group">
            <asp:Label runat="server" Text="Option 1"></asp:Label><br />
            <input runat="server" type="text" maxlength="64" id="questionOption2"
              class="form-control" />
            <input runat="server" type="checkbox" id="questionOption2Validity" 
              class="input-group-append"/>
            <asp:RequiredFieldValidator runat="server" ControlToValidate="questionOption2"
              ErrorMessage="Option 2 is required!" CssClass="text-danger">
            </asp:RequiredFieldValidator>
          </div>
          <div class="d-flex flex-row-reverse">
            <button class="btn btn-sm btn-primary">Add another Option</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</asp:Content>

new_quiz.aspx.cs new_quiz.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace onlineQuiz_bsef17m35.teacher_quizes
{
  public partial class new_quiz : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack)
      {
        return;
      }

      DatabaseEntities db = new DatabaseEntities();
      var quizes = db.getQuestionTypes().ToArray();
      questionType.DataSource = quizes;
      questionType.DataBind();

      if (questionType.Value == "Multiple Choice")
      {
        questionOptions.Visible = true;
      } else
      {
        questionOptions.Visible = false;
      }


      /* register client scripts */
      Type scriptType = this.GetType();
      String scriptName = "script";
      String scriptUrl = "./new_quiz.js";
      String scriptText = File.ReadAllText(Server.MapPath(scriptUrl));
      ClientScriptManager scriptManager = Page.ClientScript;
      if (!scriptManager.IsClientScriptBlockRegistered(scriptType, scriptName))
      {
        scriptManager.RegisterClientScriptBlock(scriptType, scriptName, scriptText, true);
      }
    }
  }
}

new_quiz.js new_quiz.js

function questionTypeChange() {
  const element = document.getElementById("<%= question.ClientID %>");
  console.log(element);
}

First check if your jQuery is loaded correctly.首先检查您的 jQuery 是否加载正确。 Open console (press F12) and type "jQuery" and enter - you should get something back.打开控制台(按 F12)并输入“jQuery”并输入 - 你应该得到一些东西。 If you get "not defined" - then make sure you do load it.如果你得到“未定义” - 然后确保你加载它。

Then, you should be able to access your HTML, via jQuery selector.然后,您应该能够通过 jQuery 选择器访问您的 HTML。

More info here: https://www.w3schools.com/jquery更多信息在这里: https://www.w3schools.com/jquery

Real problem was that I was using external script.真正的问题是我使用的是外部脚本。 ASP.NET Tag in external script does not work.外部脚本中的 ASP.NET 标记不起作用。 For that I used jQuery pattern matching to find controls.为此,我使用jQuery模式匹配来查找控件。

To find button with ID="saveButton" , I used $("[id$=saveButton]") that results in all Controls having an ID ending with saveButton .为了找到ID="saveButton"的按钮,我使用$("[id$=saveButton]") ,这会导致所有控件的 ID 都以saveButton结尾。

Above works, because ASP.NET appends original ID to some random ID generated while compilation, so final html id always ends with original id given in markup.以上工作,因为 ASP.NET 将原始 ID 附加到编译时生成的一些随机 ID,所以最终 html id 总是以标记中给出的原始 id 结尾。

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

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