简体   繁体   English

如何在Server Control / C#中获取ID?

[英]How to get ID in Server Control / C#?

I try to write a simple Server Control to control the Banners in my site. 我尝试编写一个简单的服务器控件来控制网站中的横幅广告。

It should be somethink like: 应该是这样的:

<Banners:mng_bnr ID="Upper_bnr / aside_bnr / bottom_bnr / etc" runat="server" />

And then the Control will be something like: 然后,控件将类似于:

    <%@ Control Language="C#" %>

<script runat="server">

 protected void Page_Load(object sender, EventArgs e)
 {


 switch(which_banner)
 {
 case which_banner.upper_banner_wide: 
<!-- Ad code -->
 <script type="text/javascript"><!--
JS code
 </script>
 <script type="text/javascript"

 </script>
 <!-- *** Ad Code *** --> 
break;

case which_banner.upper_banner_small:
 <!-- Ad code -->
 <script type="text/javascript"><!--
JS code
 </script>
 <script type="text/javascript"

 </script>
 <!-- *** Ad Code *** --> 
break;

case which_banner.aside_336_280_top:

 <!-- Ad code -->
 <script type="text/javascript"><!--
JS code
 </script>
 <script type="text/javascript"

 </script>
 <!-- *** Ad Code *** -->

But, I don't know how to get and handle the ID of each control (there should be around 5-6 controls on each page which every one will have it own ID - which mean different banner) 但是,我不知道如何获取和处理每个控件的ID(每个页面上应该有大约5-6个控件,每个控件都有自己的ID-表示不同的横幅)

Thanks a lot. 非常感谢。

This post is overly vague. 这个帖子太含糊了。 I think what you are asking is how to pass the ID's of your banner control from the parent page into this control. 认为您要问的是如何将横幅广告控件的ID从父页面传递到此控件中。 In that case you would do something like this: 在这种情况下,您将执行以下操作:

 private string _bannerId = string.Empty;
 public string BannerId
 {
      get { return _bannerId; }
      set { _bannerId = value; }
 }

Then your switch looks like this: 然后您的开关如下所示:

 switch(_bannerId)
 {
     case "banner1":
     break;

     case "banner2":
     break; 
 }

Then on your parent page you would do something like this: 然后在您的父页面上,您将执行以下操作:

 <Banners:mng_bnr BannerId="Upper_bnr" runat="server" />

If you want to pass in multiple banner id's I would suggest either using a collection or providing a property for each banner but this really depends on what you are trying to achieve. 如果您想传递多个横幅ID,我建议您使用一个集合或为每个横幅提供一个属性,但这实际上取决于您要实现的目标。

Hope this helps. 希望这可以帮助。

--Edit-- - 编辑 -

In terms of writing out the necessary code there are plenty of ways you can do that. 在编写必要的代码方面,有很多方法可以做到。 You could, for example, do something like this: 例如,您可以执行以下操作:

switch(_bannerId)
{
 case "banner1":
     scriptLiteral.Text = "<script>...</script>";
 break;

 case "banner2":
     scriptLiteral.Text = "<script>...</script>";
 break; 
}

And then on your control you would have 然后您可以控制

<asp:Literal id="scriptLiteral" runat="server" />

I would probably use RegisterClientScriptBlock though. 我可能会使用RegisterClientScriptBlock

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

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