如何在C#代码后面获取动态创建的控件的ID [英] how do I get the ID of my dynamically created controls in C# code behind

查看:368
本文介绍了如何在C#代码后面获取动态创建的控件的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type = "text/javascript">
       var counter = 0;
       function AddFileUpload() {
           counter++;
           var div = document.createElement('DIV');

           div.innerHTML = '<input id="file' +
     counter + '" name = "file' +
     counter + '" type="file" />  Name Of Certificate : <input  id="TextBox' +
     counter + '" name="TextBox' +
     counter + '" type="text" />   Year Obtained : <input  id="TextBox2' +
     counter + '" name="TextBox2' +
     counter + '" type="text" />   <input id="Button' +
     counter + '" type="button" value="Remove"  önclick = "RemoveFileUpload(this)" />';

           document.getElementById("FileUploadContainer").appendChild(div);
       }
       function RemoveFileUpload(div) {
           document.getElementById("FileUploadContainer").removeChild(div.parentNode);
       }
</script>



我的C#代码


my C# code

protected void PersonalButtonNext3_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        BSEnterpriseLib dal = new BSEnterpriseLib();
        string msg = "";
       

    for (int i = 0; i < Request.Files.Count; i++)
    {
        HttpPostedFile PostedFile = Request.Files[i];
        if (PostedFile.ContentLength > 0)
        {
            string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
            
//here I need to get the ID of the text boxes and get the data inputed into them and then use //this data to input into the database, while iterating     

            //insert into the database here
            PostedFile.SaveAs(Server.MapPath("Uploaded Documents\\") + FileName);
        }
    }

    messagelabel.text = "Successful";
    }

推荐答案

protected void PersonalButtonNext3_Click(object sender,EventArgs e)

{

DataSet ds = new DataSet();

BSEnterpriseLib dal = new BSEnterpriseLib();

string msg =;

$ / b
for(int i = 0; i< Request.Files.Count; i ++)

{

HttpPostedFile PostedFile = Request.Files [i];

if(PostedFile.ContentLength> 0)

{

string FileName = System.IO.Path.GetFileName(PostedFile.FileName);

FileName = Session [StaffIDforTabs]。ToString()+_+ FileName;



var SChoolAttended = Request.Form [TextBox2+ i];

var NameOfCertificate = Request.Form [TextBox+ i];

var YearObtained = Request.Form [TextBox3+ i] ;





//这里插入数据库

dal.InsertApplicationAcademicQualification(FileName,〜/ Uploaded Documents /+ FileName,Convert.ToInt32(Session [StaffIDforTabs]),SChoolAttended,YearObtained,NameOfCertificate,ref msg);





if(msg!=)

{

MessageBox.Show(由于+ msg无法上传证书);

返回;

}





















PostedFile.SaveAs(Server.MapPath(Uploaded Documents\\)+ FileName);

}

}

< br $>


ShowError(成功);

MessageBox.Show(成功);

WebTab1.SelectedInde x = 2;



}
protected void PersonalButtonNext3_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
BSEnterpriseLib dal = new BSEnterpriseLib();
string msg = "";


for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
if (PostedFile.ContentLength > 0)
{
string FileName = System.IO.Path.GetFileName(PostedFile.FileName);
FileName = Session["StaffIDforTabs"].ToString() + "_" + FileName;

var SChoolAttended = Request.Form["TextBox2" + i];
var NameOfCertificate = Request.Form["TextBox" + i];
var YearObtained = Request.Form["TextBox3" + i];


//insert into the database here
dal.InsertApplicationAcademicQualification(FileName, "~/Uploaded Documents/" + FileName, Convert.ToInt32(Session["StaffIDforTabs"]), SChoolAttended, YearObtained, NameOfCertificate, ref msg);


if (msg != "")
{
MessageBox.Show("Unable to Upload Certificates as a result of " + msg);
return;
}










PostedFile.SaveAs(Server.MapPath("Uploaded Documents\\") + FileName);
}
}


ShowError("Successful");
MessageBox.Show("Successful");
WebTab1.SelectedIndex = 2;

}


您无法获取这些动态控件的值因为它们是在客户端使用 JavaScript 创建的。

而是从 Code Behind 创建它们。



或者您可以在 aspx 页面中定义它们,其可见性 false (通过设置 style =display:none;)。在那个 JavaScript 函数中,只需使它们可见。
You can't get the values of these Dynamic Controls as they are getting created at Client Side using JavaScript.
Instead create them from Code Behind.

Or you can define them in aspx page with its visibility false (by setting style="display:none;"). In that JavaScript function, just make them visible.


这篇关于如何在C#代码后面获取动态创建的控件的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆