如果Null我想要显示dataitem0
<asp:Label ID="Label18" Text='<%# Eval("item") %>' runat="server"></asp:Label>
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?
在我的ASP.NET表单中回发(单击一个按钮)后,我的表单的所有DataItem都为null.为什么?即使在回发后,我该怎么做才能检索DataList的内容?
protected void buttonAddRecord_Click(object sender, EventArgs e)
{
foreach (DataListItem item in listFields.Items)
{
// item.DataItem == null WTF?
}
}
protected void Page_Load(object sender, EventArgs e)
{
BindFields();
}
private void BindFields()
{
object setting = MySettings.GetSetting();
if (!Null.IsNull(setting))
{
listFields.DataSource =
DataProvider.GetData(int.Parse(setting.ToString()));
listFields.DataBind();
}
listFields.Visible = listFields.Items.Count > 0;
emptyMessage.Visible = listFields.Items.Count == 0;
}
Run Code Online (Sandbox Code Playgroud) 这是我的自定义服务器控件的示例代码:
[Designer(typeof(ContainerControlDesigner))]
[ToolboxData("<{0}:BlocArrondi runat=server><ContenuPrincipal></ContenuPrincipal></{0}:BlocArrondi>")]
public class BlocArrondi : WebControl
{
private ITemplate _ContenuPrincipal;
protected Panel _PanelContenuPrincipal = new Panel();
public BlocArrondi()
: base(HtmlTextWriterTag.Div)
{
}
[PersistenceMode(PersistenceMode.InnerProperty)]
[TemplateInstance(TemplateInstance.Single)]
public ITemplate ContenuPrincipal
{
get { return _ContenuPrincipal; }
set { _ContenuPrincipal = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_PanelContenuPrincipal.ID = "PanelPrincipal";
this.Controls.Add(_PanelContenuPrincipal);
if (_ContenuPrincipal != null)
_ContenuPrincipal.InstantiateIn(_PanelContenuPrincipal);
}
}
Run Code Online (Sandbox Code Playgroud)
在这里实施:
<controls:BlocArrondi runat="server">
<ContenuPrincipal>
<asp:Label id="LabelInfo" runat="server" />
</ContenuPrincipal>
</controls:BlocArrondi>
Run Code Online (Sandbox Code Playgroud)
我的标签LabelInfo可以在后面的代码上访问,太棒了!
但是,如果我在Repeater或ListView中使用我的自定义控件,我不能使用ContenuPrincipal模板中的Container.DataItem属性:
<asp:Repeater id="RepeaterInfos" runat="server">
<ItemTemplate>
<controls:BlocArrondi runat="server">
<ContenuPrincipal> …Run Code Online (Sandbox Code Playgroud) 我是ASP.Net的新手,我不确定我是否会以正确的方式解决这个问题.我有一个Repeater,它绑定到一个"图像"对象列表.在每个RepeaterItem内是一个复选框,我有一个按钮OnClick事件,我想显示已检查的Image对象的一些属性.
标签更新,但元数据为空.DataBinder.Eval(i.DataItem,"FileName")将返回null,但我不确定为什么?我想也许复选框的回发导致了问题,但是如果我尝试在发生任何回发之前显示数据,我仍然会遇到同样的问题,所以也许我没有正确地获取属性.或者我是以完全错误的方式解决这个问题?任何帮助赞赏.
码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string importPath = Server.MapPath("~/Images/ForImport");
ImageProcessor processor = new ImageProcessor(importPath);
rptImageList.DataSource = processor.ImageList;
rptImageList.DataBind();
}
}
protected void btnImport_Click(object sender, EventArgs e)
{
foreach (RepeaterItem i in rptImageList.Items)
{
CheckBox chk = i.FindControl("chkSelectImage") as CheckBox;
if (chk.Checked)
{
Testlabel.Text += "Selected: " + DataBinder.Eval(i.DataItem, "FileName");
}
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<asp:Repeater ID="rptImageList" runat="server">
<ItemTemplate>
<div class="photoinstance">
<asp:Image runat="server" ImageUrl='<%#"Images/ForImport/" +DataBinder.Eval(Container.DataItem, "FileName") %>' />
<asp:CheckBox ID="chkSelectImage" AutoPostBack="true" runat="server"/>
<p><%#Eval("FileName")%> - …Run Code Online (Sandbox Code Playgroud) 在网页中,在加载时,我用两个表填充数据集,其中包含这些表之间的关系,然后将数据加载到具有嵌套转发器的转发器中.用户单击按钮后也会发生这种情况.数据从SQL数据库加载,并且在回发后将转发器数据源设置为数据集.但是,当ItemDataBound发生时,Item.Dataitem始终为null.
为什么会这样?
这是数据绑定代码:
this.rptCustomSpaList.DataSource = ds;
this.rptCustomSpaList.DataBind();
Run Code Online (Sandbox Code Playgroud)
这是ItemDataBound代码:
RepeaterItem item = e.Item;
Repeater ChildRepeater = (Repeater)item.FindControl("rptCustomSpaItem");
DataRowView dv = e.Item.DataItem as DataRowView;
ChildRepeater.DataSource = dv.CreateChildView("sparelation");
ChildRepeater.DataBind();
Run Code Online (Sandbox Code Playgroud)
下面是我的HTML转发器代码
<asp:Repeater ID="rptCustomSpaList" runat="server"
onitemdatabound="rptCustomSpaList_ItemDataBound">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td><asp:Label ID="Label3" runat="server" Text="Spa Series:"></asp:Label></td>
<td><asp:Label ID="Label4" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SPASERIESVALUE") %>'></asp:Label></td>
</tr>
<tr>
<td><asp:Label ID="Label5" runat="server" Text="Spa Model:"></asp:Label></td>
<td><asp:Label ID="Label6" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "SPAMODELVALUE") %>'></asp:Label></td>
</tr>
<tr>
<td><asp:Label ID="Label9" runat="server" Text="Acrylic Color:"></asp:Label></td>
<td><asp:Label ID="Label10" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ACRYLICCOLORVALUE") %>'></asp:Label></td>
</tr>
<tr>
<td>
<asp:Label ID="Label11" runat="server" Text="Cabinet Color:"></asp:Label> …Run Code Online (Sandbox Code Playgroud) 我将我的转发器的DataSource设置为List(MyProducts是一个简单的类,仅包含get/setters).
在此和DataBind()之后,我可以在调试模式中看到每个Repeater.Items的DataItem为null.在进行回发并尝试更新MyProducts时,Repeater.Items [n] .DataItem仍为null,我无法投射它,以完成我的工作.
当我对转发器进行数据绑定时,为什么不在每个RepeaterItem上设置DataItem?我不能想/谷歌出来.我的代码的每个其他方面都正常工作(将数据从MyProducts输出到aspx,例如:
<asp:TextBox runat="server" id="q" Text='<%# DataBinder.Eval(Container.DataItem, "Quantity")%>'></asp:TextBox>
Run Code Online (Sandbox Code Playgroud)
更多代码:
public class MyProducts
{
public string Number
{
get; set;
}
public decimal Price
{
get; set;
}
public decimal Quantity
{
get; set;
}
public decimal Total
{
get { return Quantity * Price; }
}
}
Run Code Online (Sandbox Code Playgroud)
正在生成:
public List<MyProducts> TheProducts
{
get { // Invoking webservice, getting response as xml and converting it to a list of MyProducts }
}
Run Code Online (Sandbox Code Playgroud)
我的用户控制:
// Bind products in cart …Run Code Online (Sandbox Code Playgroud) 如何从处于编辑模式的GridView行获取基础DataItem,并且用户单击"更新"?
我有一个可编辑的GridView.我已将CustomValidator添加到其中一列的Edit视图中.当用户单击Update I时,运行服务器验证方法并需要获取正在编辑的行的基础DataItem,以便我可以获取数据项的主键以用于验证.
GridView1.Rows[GridView1.EditIndex].DataItem;
Run Code Online (Sandbox Code Playgroud)
这不起作用.我猜是因为GridView没有在update命令上进行数据表示?
我有一个 gridview,我以编程方式为其设置数据源并将其数据绑定到对象集合。对于创建的每一行,我然后在字段中使用不同的方法从对象中提取相关信息,如下所示:
<asp:TemplateField HeaderText="Aliases">
<ItemTemplate>
<%# ( (MyItem)Container.DataItem).Aliases.ToString() %>
</ItemTemplate>
</asp:TemplateField>
Run Code Online (Sandbox Code Playgroud)
我的问题是,在该OnRowDeleting方法中,我希望能够DataItem使用 egMyGridView.Rows[e.RowIndex].DataItem或以其他方式访问该方法。但我找不到如何配置Gridview以保留DataItem. 是否可以访问DataItem使用的,我将如何配置它来做到这一点?如果这是不可能的,我可以访问由方法绑定的值吗?或者我是否必须采用计划 B 并将数据源对象集合重写为数据表,然后使用数据键名?
我正在尝试 dataitem APi,这是我的代码
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle connectionHint) {
Log.d("Inside", "onConnected: " + connectionHint);
Toast.makeText(getApplicationContext(),"Inside On connected",Toast.LENGTH_SHORT).show();
// Now you can use the Data Layer API
//Creating Dataitem
PutDataMapRequest dataMapRequest = PutDataMapRequest.create("/count");
dataMapRequest.setUrgent();
DataMap datamap=dataMapRequest.getDataMap();
PutDataRequest putDataRequest=dataMapRequest.asPutDataRequest();
datamap.putString("key", "Value");
PendingResult<DataApi.DataItemResult> pendingResult =
Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
}
@Override
public void onConnectionSuspended(int cause) {
Log.d("Inside", "onConnectionSuspended: " + cause);
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d("Inside", "onConnectionFailed: " + result);
} …Run Code Online (Sandbox Code Playgroud)