用于从转发器访问控件内部的动态ID [英] For accessing dynamic id inside in a control from repeater

查看:126
本文介绍了用于从转发器访问控件内部的动态ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生/女士,
我想在我的aspx页面中的代码后面访问动态控件ID,但我没有获得如何获取动态ID的信息.



Sir/Madam,
I want to access dynamic controls id in code behind in my aspx page but i didn''t get it how to get my dynamic id.



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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:repeater ID="myRepeater" runat="server"

            onitemdatabound="myRepeater_ItemDataBound"

            onitemcommand="myRepeater_ItemCommand">
            <ItemTemplate>
                  <asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" UseSubmitBehavior="false" runat="server" Text="Button" CommandName="myfunction" />
            </ItemTemplate>

            </asp:repeater>
    </div>
    </form>
</body>
</html>




还有我的代码背后的代码




and my code behind code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.IO;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
    public static string Connection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True;User Instance=True";
    public string code;
    public string id
    {
        get
        { return id; }
        set
        {  id=value; }
    

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection con = new SqlConnection(Connection);
        con.Open();
        System.Data.SqlClient.SqlCommand cmd = new SqlCommand("select * from COMPLAINS", con);
        System.Data.SqlClient.SqlDataAdapter dp = new SqlDataAdapter(cmd);
        System.Data.DataTable dtu = new DataTable();
        dp.Fill(dtu);
        myRepeater.DataSource = dtu;
        myRepeater.DataBind();
        
    }
    protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        var textbox = e.Item.FindControl("myTextBox");
        textbox.ClientIDMode = ClientIDMode.Static;
        textbox.ID = "myTextBox" + (e.Item.ItemIndex + 1);
        //
    }
    protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "myfunction")
        {  
            string textbox = e.Item.ItemIndex.ToString();
            textbox = "myTextBox" + textbox;
            var nme = e.Item.FindControl(textbox);
            
        }
    }
}



我已经创建了我的文本框ID,例如myTextBox1,myTextBox2,myTextBox3等itemdatabound.但是现在我想访问我在itemcommand上的文本框ID,但是我无法在后面的代码中访问我的ID.当此方法显示为空值时,



i have create my textbox id''s like myTextBox1,myTextBox2,myTextBox3 and so on itemdatabound . but now i want to access my textbox id''s on itemcommand but i can''t access my id in code behind.it says null value when this method

if (e.CommandName == "myfunction")
        {
            string textbox = e.Item.ItemIndex.ToString();
            textbox = "myTextBox" + textbox;
            var nme = e.Item.FindControl(textbox);

        }


请帮助我


please help me

推荐答案

分配ID时,分配ItemIndex + 1,但是在命令事件中访问控件时,则使用ItemIndex.因此,您的第一个框是myTextBox1,但是您的代码将查找myTextBox0.

When you assign the IDs you assign ItemIndex+1, but when you access the control in your command event you use ItemIndex. So your first box is myTextBox1 but your code looks for myTextBox0.

protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "myfunction")
    {
        // add the "+ 1" here too
        string textbox = (e.Item.ItemIndex + 1).ToString();


这篇关于用于从转发器访问控件内部的动态ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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