【问题标题】:How to databind a repeater from stored procedure?如何从存储过程中对转发器进行数据绑定?
【发布时间】:2011-11-11 21:09:37
【问题描述】:

我正在尝试对我的中继器进行数据绑定,但到目前为止还没有任何运气。任何人都认为这可以告诉我我要去哪里错了吗?通过遵循一些教程/示例,我目前有两个功能,但我希望只有一个......也许不可能。谢谢!

HTML:

                <ItemTemplate>
                        <tr class="row">
                            <td><asp:Label ID="TitleLabel" runat="server" Text=""></asp:Label></td>
                            <td><asp:Label ID="NameLabel" runat="server" Text=""></asp:Label></td>
                            <td><asp:Label ID="PhoneLabel" runat="server" Text=""></asp:Label></td>
                            <td><asp:Label ID="EmailLabel" runat="server" Text=""></asp:Label></td>
                        </tr>
                </ItemTemplate>

VB

Protected Sub BindData()

    Dim oCommand As SqlCommand
    Dim oReader As SqlDataReader

    Try
        oCommand = DataAccess.GetSQLCommand("People_Retrieve", CommandType.StoredProcedure, SourceServer.ConnectionLocal)
        oCommand.Connection.ChangeDatabase("MyDatabase")

        oCommand.CommandTimeout() = 9000
        oReader = oCommand.ExecuteReader()

        PeopleRepeater.DataSource = oReader
        PeopleRepeater.DataBind()

    Catch ex As Exception
        ErrorHandler.HandleError(ex)
    Finally
        oReader.Close()
        oReader = Nothing
    End Try

End Sub

Protected Sub PeopleRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles PeopleRepeater.ItemDataBound

    Dim NameLabel As Label = CType(e.Item.FindControl("LabelName"), Label)
    NameLabel.Text = e.Item.DataItem("Name")

    Dim TitleLabel As Label = CType(e.Item.FindControl("TitleName"), Label)
    NameLabel.Text = e.Item.DataItem("Title")

    Dim PhoneLabel As Label = CType(e.Item.FindControl("PhoneName"), Label)
    NameLabel.Text = e.Item.DataItem("Phone")

    Dim EmailLabel As Label = CType(e.Item.FindControl("EmailName"), Label)
    NameLabel.Text = e.Item.DataItem("Email")

End Sub

【问题讨论】:

    标签: asp.net vb.net repeater data-binding


    【解决方案1】:

    使用SqlDataAdapter

    Using adap As New SqlDataAdapter(oCommand)
        Dim table As New DataTable()
        adap.Fill(table)
    
        PeopleRepeater.DataSource = table
        PeopleRepeater.DataBind()
    End Using
    

    我也看不到您在哪里打开连接,因此您可能需要添加:

    oCommand.Connection.Open()
    

    【讨论】:

    • 进行 VB.NET 转换。
    【解决方案2】:

    按照步骤进行

    1. 创建名为“SelectPersonalDetails”的存储过程

      创建过程选择个人详细信息

      -- 在此处添加存储过程的参数

      @电子邮件系统名

      AS
      
      BEGIN
      

      -- 添加了 SET NOCOUNT ON 以防止额外的结果集 -- 干扰 SELECT 语句。

      设置无计数;

      -- 在此处插入过程语句

      开始尝试 开始交易

      开始

      从个人详细信息中选择姓名、职务、电话、电子邮件

      在哪里 电子邮件 = @Email

      结束

      提交交易

      结束尝试

      开始捕捉

      ROLLBACK TRANSACTION
      DECLARE @ERR AS VARCHAR(500)
      SELECT @ERR = ERROR_MESSAGE()   
      RAISERROR(@ERR,16,1)
      
      RETURN
      

      结束捕获 结束

    2. 创建数据集以绑定转发器中的数据。

      公共数据集 Get_PersonaldetailbasedEmail() {

          try
          {
              DataSet oDS = new DataSet();
              SqlParameter[] oParam = new SqlParameter[1];
      
              oParam[0] = new SqlParameter("@Email", _sEmail);
      
      
              oDS = SqlHelper.ExecuteDataset(DataConnectionString,          CommandType.StoredProcedure, "SelectPersonalDetails", oParam);
              return oDS;
          }
          catch (Exception e)
          {
              ErrorMessage = e.Message;
              return null;
          }
      }
      

    注意:(i) SelectPersonalDetails 是存储过程名称

      (ii) In order to select unique record from the table i have used emailid 
    
      (iii) I have assume the table name as PersonalDetails.
    
    1. 创建一个类似于 Personeldetails.ascx 的用户控制页面

      /li> /li> /li> /li>

    注意上面我有转发器的 html 代码,但我不知道如何在这个编辑器中工作。无论如何,中继器 ID 与您的中继器相同,标签 ID 与您的标签 ID 相同。

    1. 数据绑定

      创建一个函数来绑定数据

      public void FillArray(ArrayList alist) { ArrayList al = new ArrayList();

          foreach (Object objRow in alist)
          {
              string sTitle = ((DataRow)objRow)["Title"].ToString();
              string sName = ((DataRow)objRow)["Name"].ToString();
              string sPhone = ((DataRow)objRow)["Phone"].ToString();
             string sMail = ((DataRow)objRow)["Mail"].ToString();        
      
              al.Add(new string[]{ sTitle,sName,sPhone,sMail});
      
      
          }
          PeopleRepeater.DataSource = al;
          PeopleRepeater.DataBind();
      
      
      }
      
    2. 现在称为项目数据绑定

      如果(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { string[] itemArray = ((string[])e.Item.DataItem);

              Label myTitle = (Lable)e.Item.FindControl("TitleLabel");
              Label myName = (Label)e.Item.FindControl("NameLabel");
              Label myPhone = (Label)e.Item.FindControl("PhoneLabel");
              Label myEmail = (Label)e.Item.FindControl("EmailLabel");
      
              myTitle.Text = itemArray[0];
              myName.Text = itemArray[1];
              myPhone.Text = itemArray[2];
              myEmail.Text = itemArray[3];
      
      
      
      
          }
      

    如果您觉得答案有用,请将其标记为您的答案,否则请告诉我......

    【讨论】:

    • 我觉得这有点太本地化了。我会尝试将其缩减为仅 OP 要求的内容。
    • 这个问题也被标记为 VB.NET。
    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 2013-02-06
    相关资源
    最近更新 更多