1
我用一個調用存儲過程的sqldatasource創建了一個gridview用戶控件。用戶控件是在aspx頁面上動態創建的,我擁有一切工作而無需傳入任何參數。我無法弄清楚如何在用戶控件中傳遞參數。這是代碼的最小化版本。將一個sql參數傳遞給一個動態創建的用戶控件C#
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Survey.ascx.cs" Inherits="AppSurvey" %>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="Count" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:TextBox ID="txtAnswerCount" runat="server" Columns="5" MaxLength="5" />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ValidationExpression="^\d+$"
Display="None" SetFocusOnError="true" ControlToValidate="txtAnswerCount" ErrorMessage = "Count values must be entered as integer values." />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" CancelSelectOnNullParameter="false" ConnectionString="<%$ ConnectionStrings:1316 %>" SelectCommand="dbo.usp_Survey" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="Category" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Configuration;
namespace EUCNET01316
{
public partial class Survey : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters["Category"].DefaultValue = ?????;
GridView1.DataSourceID = "SqlDataSource1";
GridView1.DataBind();
}
}
}
**** ASPX Page
protected void Page_Load(object sender, EventArgs e)
{
UserControl ctl = Page.LoadControl("Survey.ascx") as UserControl;
Panel PB = new Panel();
PB.Controls.Add(ctl);
}
對不起,提出這樣一個愚蠢的問題,但這是我嘗試使用的第一個用戶控件,我不知道你的意思是「設置你在用戶控件上創建的屬性」。 – Mike
更新示例 – DVK
謝謝!!!!!你太棒了!!!!!我花了很多時間試圖做這項工作。 – Mike