“FindControl”问题在DataList中 [英] Problem with "FindControl" in DataList

查看:94
本文介绍了“FindControl”问题在DataList中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm stuck in a very irritating problem. I tried this thing two months earlier also but had to change the approach after failure as I was in a hurry to complete the project. Now I'm in a vacation and want to solve the problem. I'm fed up with this. I'eve tried from many sources on the web. Most of the sources refer to the procedure I pasted below, but that doesn't work in my code. I wanna know what's wrong. :( Please please please help!!

the .aspx code is here :







<%@ Page Title="" Language="C#" MasterPageFile="~/SitePages/Master_LoggedIn.master" AutoEventWireup="true" 



CodeFile="ChooseExam.aspx.cs" Inherits="ChooseExam" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">

<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" OnItemCommand="DataList1_ItemCommand" 



RepeatDirection="Horizontal" Width="100%">
            <HeaderTemplate>
                <div style="text-align: center; height: 38px; background-color: #FFFFCC">
                    <span style="font-family: 'Bookman Old Style'; color: #003300">Click On 
                    The Test You Want To Take</span></div>
            </HeaderTemplate>
            <ItemTemplate>
                <table class="style1">
                    <tr>
                        <td>
                            <asp:LinkButton ID="LinkButton_ExamName" runat="server" BorderColor="Black"  

                                

                                style="font-family: 'Bookman Old Style'; color: #000000; text-decoration: none;"><%#Eval

("ExamName")%></asp:LinkButton>
                                
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>

<p>
The Chosen Exam is <asp:Label ID="Label_ExamName" runat="server" 

                                style="font-family: 'Bookman Old Style'"></asp:Label>
</p>

</asp:Content>







and here's the .aspx.cs code:







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

public partial class ChooseExam : System.Web.UI.Page
{
    ClassOnlineExamConnection con = new ClassOnlineExamConnection();
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        Label_ExamName.Visible = false;
        ds = new DataSet();
        ds = con.dataFetch("Select ExamCode, ExamName From Exams");

        DataList1.DataSource = ds;
        DataList1.DataBind();
    }

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.SelectedIndex = e.Item.ItemIndex;
        String A = ((LinkButton)e.Item.FindControl("LinkButton_ExamName")).Text;
        Label_ExamName.Text = A;
        Label_ExamName.Visible = true;
    }
}







thank a lot in advance. I'll be grateful to you..

推荐答案

不要在每个帖子中绑定数据,请按以下步骤操作

don't bind data in each post back, do as below
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Label_ExamName.Visible = false;
        ds = new DataSet();
        ds = con.dataFetch("Select ExamCode, ExamName From Exams");
    
        DataList1.DataSource = ds;
        DataList1.DataBind();
    }
}



同时更改链接按钮如下


Also change your link button as below

<asp:LinkButton ID="LinkButton_ExamName" CommandName="Select" CommandArgument='<%#Eval("ExamName")%>' runat="server" BorderColor="Black" tyle="font-family: 'Bookman Old Style'; color: #000000; text-decoration: none;"><%#Eval("ExamName")%></asp:LinkButton>





然后





then

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if(e.CommandName ="Select")
    {
        var e = e.CommandArgument.ToString();
        Label_ExamName.Text = e;
        Label_ExamName.Visible = true;
    }
}


这篇关于“FindControl”问题在DataList中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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