Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过C查找输入类型的Id和文本#_C#_Jquery_Asp.net - Fatal编程技术网

C# 通过C查找输入类型的Id和文本#

C# 通过C查找输入类型的Id和文本#,c#,jquery,asp.net,C#,Jquery,Asp.net,我已经在一个html页面中保存了输入(文本、复选框、下拉列表),并且正在asp:panel中加载,加载后用户可以在其中输入值。现在通过点击Asp:按钮,我必须找到输入类型并保存到数据库,这是如何实现的 输入代码将采用此格式 <label style="left: 46px; top: 73px; position: relative;" id="Llb1" onmouseup="MLup(this.id)" class="ui-draggable" onmousedown="MLdown(

我已经在一个html页面中保存了输入(文本、复选框、下拉列表),并且正在asp:panel中加载,加载后用户可以在其中输入值。现在通过点击Asp:按钮,我必须找到输入类型并保存到数据库,这是如何实现的

输入代码将采用此格式

<label style="left: 46px; top: 73px; position: relative;" id="Llb1" onmouseup="MLup(this.id)" class="ui-draggable" onmousedown="MLdown(this.id)"> Enter the value </label>
<input style="left: 170px; top: 113px; position: relative;" id="T1" onmouseup="mUp(this.id)" class="ui-draggable"  onmousedown="mDown(this.id)" value="" type="text">
输入值

这个输入类型我必须找到那些ID和文本。。。怎么可能…

首先,您无法在服务器上获取标签的值。要获取动态文本框值,您需要在其上指定
名称
属性,并在从
请求回发时获取值。按该名称形成
集合。

您至少有两个选项来实现它

  • 在创建字段的同时创建一些隐藏字段,并将字段类型值和其他必需的元数据放入隐藏字段中。发布表单时,这些隐藏字段将为您提供有关表单的元数据,您可以使用这些元数据将表单正确插入数据库

  • 动态创建控件时,请使用ajax请求通知服务器,以便服务器事先了解这些字段。您可以将此信息存储在会话中,当表单发回时,您可以使用它从请求对象获取数据
在任何一种情况下,您都必须使用原始请求对象来获取字段的值,因为asp.net不会解析那些不在页面上的字段,因为它们不会是视图状态的一部分


最好使用ajax发布整个表单,而不是以常规方式发布。通过这种方式,您可以确保只发布动态数据。

这里我找到了一个通过html按钮单击查找文本、选择、标签id的解决方案

 <input type="button" id="save" value="submit" onclick="submitpage()" />

然后在提交页面上

<script type="text/javascript">
          function submitpage() {
              var completeids = new Array();
              var completevalues = new Array();
              var completetype = new Array();
              var completeselected = new Array();
              var labelvalues = new Array();
             // var name = "abc";
              var name = document.getElementById('<%=username.ClientID %>').innerHTML
              $('input[type="text"]').each(function () {
                  completeids.push($(this).attr('id'));
                  completetype.push('TEXTBOX');
                  completevalues.push($(this).attr('value'));
                  completeselected.push('no');

              });
              $('label').each(function () {
                  completeids.push($(this).attr('id'));
                  completevalues.push($('label[id= ' + $(this).attr('id') + ']').html());
                  labelvalues.push($('label[id= ' + $(this).attr('id') + ']').html());
                  completetype.push('LABEL');
                  completeselected.push('no');

              });
              $('select').each(function () {
                  completeids.push($(this).attr('id'));
                  completevalues.push($(this).val());
                  completetype.push('DROPDOWN');
                  completeselected.push('no');

              });
              $('input[type="checkbox"]').each(function () {
                  completeids.push($(this).attr('id'));
                  completevalues.push($('label[for=' + $(this).attr('id') + ']').html());
                  completetype.push('CHECKBOX');
                  completeselected.push($(this).attr('checked'));

              });
              $('input[type="radio"]').each(function () {
                  completeids.push($(this).attr('id'));
                  completevalues.push($('label[for=' + $(this).attr('id') + ']').html());
                  completetype.push('RADIOBUTTON');
                  completeselected.push($(this).attr('checked'));

              });
              $('input[type="file"]').each(function () {
                  completeids.push($(this).attr('id'));
                  //completevalues.push('not yet done');
                  completevalues.push($(this).attr('value'));

                  completetype.push('FILE');
                  completeselected.push('no');

              });

              var loc = window.location.href;
              $.ajax({
                  type: 'POST',
                  url: loc + "/GetSaved",
                  data: "{getcompleteids :'" + completeids + "',getcompletevalues :'" + completevalues + "',getcompletetypes :'" + completetype + "',getcompleteselected :'" + completeselected + "',getlabelvalue :'" + labelvalues + "',formname:'"+name+"'}",
                  contentType: "application/json; charset=utf-8"

              })
            .success(function (response) {
                alert(response.d);

            })
            .error(function (response) {
                alert(response.d);
            });

          }

    </script>

函数提交页(){
var completeId=新数组();
var completevalues=新数组();
var completetype=新数组();
var completeselected=新数组();
var labelvalues=新数组();
//var name=“abc”;
var name=document.getElementById(“”).innerHTML
$('input[type=“text”]”)。每个(函数(){
completeId.push($(this.attr('id'));
completetype.push('TEXTBOX');
completevalues.push($(this.attr('value'));
completeselected.push('no');
});
$('label')。每个(函数(){
completeId.push($(this.attr('id'));
completevalues.push($('label[id='+$(this.attr('id')+']')).html());
labelvalues.push($('label[id='+$(this.attr('id')+']')).html());
completetype.push('LABEL');
completeselected.push('no');
});
$('select')。每个(函数(){
completeId.push($(this.attr('id'));
completevalues.push($(this.val());
completetype.push('DROPDOWN');
completeselected.push('no');
});
$('input[type=“checkbox”]”)。每个(函数(){
completeId.push($(this.attr('id'));
push($('label[for='+$(this.attr('id')+']')).html());
completetype.push('CHECKBOX');
completeselected.push($(this.attr('checked'));
});
$('input[type=“radio”]”)。每个(函数(){
completeId.push($(this.attr('id'));
push($('label[for='+$(this.attr('id')+']')).html());
completetype.push('RADIOBUTTON');
completeselected.push($(this.attr('checked'));
});
$('input[type=“file”]”)。每个(函数(){
completeId.push($(this.attr('id'));
//push('尚未完成');
completevalues.push($(this.attr('value'));
completetype.push('FILE');
completeselected.push('no');
});
var loc=window.location.href;
$.ajax({
键入:“POST”,
url:loc+“/GetSaved”,
数据:“{getcompleteids:'”+completeids+”,getcompletevalues:“+completevalues+”,getcompletetypes:“+completetype+”,getcompleteselected:“+completeselected+”,getlabelvalue:“+labelvalues+”,formname:“+name+”}”,
contentType:“应用程序/json;字符集=utf-8”
})
.成功(功能(响应){
警报(response.d);
})
.错误(功能(响应){
警报(response.d);
});
}
在c#代码中,我用过这样的代码

 [WebMethod]
    public static string GetSaved(string getcompleteids, string getcompletevalues, string getcompletetypes, string getcompleteselected, string getlabelvalue, string formname)
    {

        string[] completeids = getcompleteids.Split(',');
        string[] completevalues = getcompletevalues.Split(',');
        string[] completetypes = getcompletetypes.Split(',');
        string[] completeselected = getcompleteselected.Split(',');
        string[] labelvalues = getlabelvalue.Split(',');
         SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dynamic"].ConnectionString);
        string FORMNAME = labelvalues[0];
        for (int i = 0; i <= completeids.Length-1; i++)
        {
            con.Open();
                                                                                        //FORM_NAME,     USER_NAME         ,CONTRO_TYPE,             CONTROL_ID,        CONTROL_VALUE,                CONTROL_SELECTED
            SqlCommand cmd = new SqlCommand("INSERT INTO CONTROLS_INFORMATION VALUES('" + FORMNAME + "',' "+formname+" ','" + completetypes[i] + "','" + completeids[i] + "','" + completevalues[i] + "','" + completeselected[i] + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
        }



        return "successfully";

    }
[WebMethod]
公共静态字符串GetSaved(字符串GetCompleteId、字符串getcompletevalues、字符串getcompletetypes、字符串getcompleteselected、字符串getlabelvalue、字符串formname)
{
字符串[]completeids=getcompleteids.Split(',');
字符串[]completevalues=getcompletevalues.Split(',');
string[]completetypes=getcompletetypes.Split(',');
字符串[]completeselected=getcompleteselected.Split(',');
字符串[]labelvalues=getlabelvalue.Split(',');
SqlConnection con=新的SqlConnection(ConfigurationManager.ConnectionString[“动态”].ConnectionString);
字符串FORMNAME=LabelValue[0];

对于(int i=0;i)为什么不将html页面替换为asp.net页面,并为输入类型赋予属性runat=“server”?实际上,输入类型是通过Jquery代码动态创建的。在这种情况下,您可能需要将动态创建的控件注册到asp.net表单。Request.form将完成此操作。