简体   繁体   English

如何使用C#/ ASP.NET访问XML对象/控件内的控件?

[英]How to access controls inside an XML object/control using C#/ASP.NET?

I'm new to XML/XSLT. 我是XML / XSLT的新手。 What I did is I've created an XML file with some list, and used XSLT to transform it. 我所做的是创建了一个带有一些列表的XML文件,并使用XSLT对其进行了转换。

Here's the code: 这是代码:

protected void Page_Load(object sender, EventArgs e)
{
    this.form1.Controls.Add(Xml1);

    Button btnSubmit = new Button();
    btnSubmit.Text = "Submit";
    this.form1.Controls.Add(btnSubmit);
    btnSubmit.Click += new System.EventHandler(btnSubmit_Click);

    Xml1.DocumentSource = "~/xml/XML_F52E2B61-18A1-11d1-B105-00805F49916B1.xml";
    Xml1.TransformSource = "~/KPI_table.xslt";

    //Together, the Xml1.DocumentSource and the Xml1.TransformSource will display a 
    //list of items with a dropdownlist each.
}

private void btnSubmit_Click(Object sender, System.EventArgs e)
{
    foreach (Control c in form1.Controls) //or is it possible to access the controls
                                          //inside XML1 here?
    {
         //This is where I need to access the controls inside the Xml1 object.
    }
}

When I tried to debug/trace the program, I found out that the foreach loop only saw 3 controls ( System.Web.UI.LiteralControl , System.Web.UI.WebControls.Xml , and System.Web.UI.WebControls.Button ). 当我尝试调试/跟踪程序时,我发现foreach循环仅看到3个控件( System.Web.UI.LiteralControlSystem.Web.UI.WebControls.XmlSystem.Web.UI.WebControls.Button )。 And I can't find a way to get into the controls inside the XXML object/control so I can get the SelectedValue of the DropdownLists/options. 而且我找不到进入XXML对象/控件内部控件的方法,因此我可以获得DropdownLists / options的SelectedValue How will I be able to access the controls inside the Xml1 object/control?? 我将如何访问Xml1对象/控件中的控件?

Update 2: Here is the generated HTML code : 更新2:这是生成的HTML代码

<body>
    <form method="post" action="Main.aspx" id="form1">
      <div class="aspNetHidden">
        <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"     value="/wEPDwULLTE2MTY2ODcyMjlkZCn80c6JtFOE8ISKTFArpEqY4qC8tA9LkNAs7gn6n6Zu" />
     </div>    
   <div class="aspNetHidden">    
      <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLm9oEzAqDAiY0LmVXxKZ8kddyJnX1LgEhJf4qGDJE14PdWmMUYC7LLAAk=" />
   </div>
   <div>
   </div>
   <table id="tbl1" runat="server" xmlns:asp="remove">    
     <tr>
       <td>BEHAVIOR</td>
     </tr>
     <tr>
       <td>Stellar KPIs</td>
     </tr>
     <tr>    
       <td>Customer Demeanor at Start of call:</td>
       <td>
         <select id="ddl_3" runat="server" AutoPostBack="true"    onchange="getvalue(this);">
           <option value="0">Select</option>
           <option value="A">                Audibly Happy</option>
           <option value="N">                Neutral</option>    
           <option value="I">                Irate</option>
           <option value="R">                At risk</option>
        </select>
      </td>
    </tr>
    <tr>
    <td>Customer Demeanor at End of call:</td>    
    <td>
      <select id="ddl_8" runat="server" AutoPostBack="true" onchange="getvalue(this);">
        <option value="0">Select</option>
        <option value="A">                Audibly Happy</option>
        <option value="N">                Neutral</option>
        <option value="I">                Irate</option>    
        <option value="R">                At risk</option>
      </select>
    </td>
  </tr>

Update 3: here's the screenshot of the HTML code rendered: screenshot 更新3:这是呈现的HTML代码的 屏幕截图 屏幕截图

Update 4: here's the XSLT file 更新4:这是XSLT文件

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="html" indent="yes"/>
    <xsl:template name="home" match="/">
        <table id="tbl1" runat="server">
            <xsl:for-each select="ProgramKPI/KPIs">
                <xsl:variable name="varKPI" select="KPI_ID"/>
                <xsl:choose>
                    <xsl:when test="Level = '1'">
                        <tr>
                            <td>
                                <xsl:value-of select="Attribute"/>
                            </td>
                        </tr>
                    </xsl:when>
                    <xsl:when test="Level = '2'">
                        <tr>
                            <td>
                                <xsl:value-of select="Attribute"/>
                            </td>
                        </tr>
                    </xsl:when>
                    <xsl:when test="Level = '3'">
                        <tr>
                            <td>
                                <xsl:value-of select="Attribute"/>
                            </td>
                            <xsl:variable name="ddl_name" select="concat('ddl_', KPI_ID)"/>
                            <td>
                                <xsl:element name="select">
                                    <xsl:attribute name="id"><xsl:value-of select="concat('ddl_', KPI_ID)"/></xsl:attribute>
                                    <xsl:attribute name="runat">server</xsl:attribute>
                                    <xsl:attribute name="AutoPostBack">true</xsl:attribute>
                                    <xsl:attribute name="onchange">getvalue(this);</xsl:attribute>
                                    <xsl:element name="option">
                                        <xsl:attribute name="value"><xsl:value-of select="0"/></xsl:attribute>
                                        <xsl:value-of select="'Select'"/>
                                    </xsl:element>
                                    <xsl:for-each select="//Parent_KPI[.=$varKPI]">
                                        <xsl:element name="option">
                                            <xsl:attribute name="value"><xsl:value-of select="preceding-sibling::AttributeCode"/></xsl:attribute>
                                            <xsl:value-of select="preceding-sibling::Attribute"/>
                                        </xsl:element>
                                    </xsl:for-each>
                                </xsl:element>
                                <!--<asp:DropDownList id="{concat('ddl_', KPI_ID)}" runat="server">
                        <asp:ListItem value="0">  Select  
                        </asp:ListItem>

                        <xsl:for-each select="//Parent_KPI[.=$varKPI]">
                          <asp:ListItem>
                            <xsl:attribute name="value">
                              <xsl:value-of select="preceding-sibling::AttributeCode"/>
                            </xsl:attribute>
                            <xsl:value-of select="preceding-sibling::Attribute"/>
                          </asp:ListItem>
                        </xsl:for-each>
                      </asp:DropDownList>-->
                            </td>
                        </tr>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

This won't work. 这行不通。

Text like <asp:DropDownList/> need to be interpreted by ASP.NET in order to be treated as controls. <asp:DropDownList/>这样的文本需要由ASP.NET解释才能被视为控件。 You're just sending that text to the browser. 您只是将文本发送到浏览器。

Try checking for child controls. 尝试检查子控件。 Ideally you'd do this recursively but as a quick and dirty way: 理想情况下,您将以递归方式进行此操作,但以一种快速而肮脏的方式进行:

private void btnSubmit_Click(Object sender, System.EventArgs e)
{
    foreach (Control c in form1.Controls) 
    {            
        if (c.HasControls())
        {
            foreach (Control child in c)
            {
                //Access Child controls here
            }
        }
    }
}

That being said I'm unsure of adding controls to the page via XML this way. 话虽这么说,我不确定以这种方式通过XML向页面添加控件。

Also you may want to add the controls earlier in the page lifecycle possibly the onInit event and make sure to not add the controls again on postback. 另外,您可能希望在页面生命周期的早期添加控件(可能是onInit事件),并确保不要在回发时再次添加控件。

UPDATE: In light of John Sanders answer: you could go old school and use the request object to access values, not a .net but it should work. 更新:根据约翰·桑德斯(John Sanders)的回答:您可以继续学习,并使用请求对象访问值,而不是.net,但它应该可以工作。 Eg: 例如:

string dropDownValue = Request.Form["yourDropDownID"];

Update 2 To Iterate through the values posted back you can do the following: 更新2要遍历回发的值,您可以执行以下操作:

private void btnSubmit_Click(Object sender, System.EventArgs e)
{
    foreach (string key in Request.Form.AllKeys)
    {
        Response.Write (string.Format("{0} => {1}<br />", key, request.Form[key]));
    }
}

However, to do this you will need to make sure that the rendered HTML has a name attribute for the form elements. 但是,为此,您需要确保呈现的HTML具有表单元素的name属性。 Change your XSLT for the select element to the following: 将select元素的XSLT更改为以下内容:

<xsl:element name="select">
   <xsl:attribute name="id">
      <xsl:value-of select="concat('ddl_', KPI_ID)"/>
   </xsl:attribute>
   <xsl:attribute name="name">
      <xsl:value-of select="concat('ddl_', KPI_ID)"/>
   </xsl:attribute>
   <xsl:attribute name="onchange">getvalue(this);</xsl:attribute>
   <xsl:element name="option">
      <xsl:attribute name="value">
         <xsl:value-of select="0"/>
      </xsl:attribute>
      <xsl:value-of select="'Select'"/>
</xsl:element>

Note the inclusion of the name attribute and the removal of the runat server and auto postback attributes. 请注意,包括了name属性,并删除了runat服务器和auto postback属性。

You say that the XML and transform produce a list of dropdown lists - are they plain html tags, or does it create asp.net controls? 您说XML和transform会生成一个下拉列表列表-它们是纯HTML标记,还是创建asp.net控件?

I seem to remember that you can only access controls from code-behind if they have an ID property and runat="server" set in the tag. 我似乎记得,如果它们具有ID属性并且在标记中设置了runat =“ server”,则只能从代码隐藏控件访问它们。

If they are plain html, can you influence the transform so that it includes an ID and runat="server" when it is applied to the xml, or is the transform out of your control? 如果它们是纯html,则可以影响转换,以便在将其应用于xml时包含ID和runat =“ server”的转换,还是该转换不受控制?

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

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