读取动态按钮ID和值 [英] Read Dynamic Button Id and Value

查看:60
本文介绍了读取动态按钮ID和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据一个月中的天数为公司花名册创建了一个带有动态按钮的动态表,例如一家公司在6月的一个月中有5名员工,即该表有150个按钮,并且在单击每个按钮时都会显示文本"OFF",我想在提交时读取该按钮的ID和值
在服务器端将按钮ID保存在数据库中

I have created a dynamic table with dynamic buttons for company roster according to the no of days in a month, like a company has 5 employees in month of june, i.e table has 150 buttons and on click of every button text appears "OFF" and i want to read that button ID and Value on submit
at server side to save button id in database

推荐答案

那是什么问题?
创建这些按钮时,还必须添加按钮单击事件(如果不添加它!).一旦添加了click事件(这可能是所有150个按钮的通用事件!),只需选中"sender"对象,您就可以使用它检索ID.
试试吧!
So what is the issue?
When you created those buttons, you must have added the button click event too (if not add it!). Once you add the click event, (which can be generic event for all 150 buttons!) just check the ''sender'' object, and you will be able to retrieve the ID using it.
Try out!


这不是确切的代码,只是一个示例.将帮助您如何找到控件并将其保存到数据库.这里只是打印

This is not exact code, only a sample. will help you how to find controls and save to database. here am just printing

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

public partial class dynamicButton : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int i = 0; i < 10; i++)
            {
                TextBox txt = new TextBox();
                txt.ID = "genTxt" + i.ToString();
                PlaceHolder1.Controls.Add(txt);
            }
        }
        else
        {
            //after postback regenerating controls
            for (int i = 0; i < 10; i++)
            {
                if (PlaceHolder1.FindControl("genTxt" + i.ToString()) == null)
                {
                    TextBox txt = new TextBox();
                    txt.ID = "genTxt" + i.ToString();
                    PlaceHolder1.Controls.Add(txt);
                }
            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Collections.Hashtable hastTab = new System.Collections.Hashtable();
        for (int i = 0; i < 10; i++)
        {
            string txtID = "genTxt" + i.ToString();
            if (PlaceHolder1.FindControl(txtID) != null)
            {
                TextBox txt = (TextBox)PlaceHolder1.FindControl(txtID);
                if (txt != null)
                {
                    hastTab.Add(txt.ID, txt.Text);
                }
            }
        }

        System.Collections.IDictionaryEnumerator IdEnum = hastTab.GetEnumerator();
        while (IdEnum.MoveNext())
        {
            Response.Write(IdEnum.Key.ToString() + "," + IdEnum.Value.ToString()+"</br>");
        }
    }
}







<pre lang="xml"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicButton.aspx.cs" Inherits="dynamicButton" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>






这篇关于读取动态按钮ID和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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