从Item模板内的Label获取文本 [英] Get text from Label inside an Item template

查看:64
本文介绍了从Item模板内的Label获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从数据库中提取数据,并使用2个标签显示正确的信息.但是,我想在后面的代码中使用这些标签,但它们对我而言并不显示.我需要在后面的代码中这样做的原因是,这样我就可以将电子邮件地址插入收件人的电子邮件中了.

I am currently pulling from the database and have the 2 labels showing the correct information. However I want to use these labels in the behind code but they are not showing up for me. The reason I need this in the behind code is so I can insert the email address to the recipient email.

下面是显示Item模板和SQL数据源的前端

Below is the front end showing the Item template and SQL Datasource

<asp:SqlDataSource ID="userCourse" runat="server" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' SelectCommand="SELECT DISTINCT tblCourse.courseTitle, tblCourse.advisorOfStudiesID, tblPersonalInfo.firstName, tblPersonalInfo.lastName, tblPersonalInfo.email FROM tblCourse INNER JOIN tblAdvisorOfStudies ON tblCourse.advisorOfStudiesID = tblAdvisorOfStudies.advisorOfStudiesID INNER JOIN tblUsers ON tblAdvisorOfStudies.userID = tblUsers.userID INNER JOIN tblPersonalInfo ON tblUsers.personalInfoID = tblPersonalInfo.personalInfoID WHERE (tblCourse.courseID = @userID)">
    <SelectParameters>
        <asp:SessionParameter SessionField="userID" Name="userID"></asp:SessionParameter>
    </SelectParameters>
</asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataSourceID="userCourse">
    <ItemTemplate>
        <asp:Label ID="aosemail" runat="server" Text='<%# Eval("email") %>' />
        <asp:Label ID="aosname" hidden runat="server" Text='<%# Eval("firstName") %>' />
    </ItemTemplate>
</asp:ListView>    

下面是我要在其中获取标签文本的后面代码

Below is the behind code in which I want to get the labels text

private bool SendEmail()
{
    string recipient = aosemail; //AOS Email textbox should go here
    MailMessage mm = new MailMessage("CSC3047@gmail.com", recipient);
    mm.Subject = "Student has chosen Manual Enrollment Process!";
    //AOS name textbox will be here
    mm.Body = "Hi "   + aosename+ "<br><br>This is a quick email to inform you that " + Name.Text.ToString() + " has chosen the manual enrollment process. The reason we are emailing you is that you are their Advisor of Studies. Please email the user on " + Email.Text.ToString() + " <br><br>Regards";
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential();
    NetworkCred.UserName = "*************";
    NetworkCred.Password = "*********";
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
    return true;
} 

预先感谢

推荐答案

您需要使用 FindContol 在ListView中搜索标签.而且由于可能不止一个,所以您必须像数组一样使用索引.

You need to use FindContol to search for the Labels in the ListView. And since there could be more than one you have to use an index, just like an Array.

int index = 0;

Label aosemail = ListView1.Items[index].FindControl("aosemail") as Label;
Label aosname = ListView1.Items[index].FindControl("aosname") as Label;

string body = "Hi " + aosname.Text + "<br><br>" + aosemail.Text;

这篇关于从Item模板内的Label获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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