how can i retrieve columns data from ListView in asp.net?
in C# can use
listView1.SelectedItems[N].SubItems[X].Text;
But Subitems[X] Doesn't exist in ASP.Net
You have to use FindControl. For instance, if you have something like this in the aspx:
<ListView id="myListView" ...>
...
<ItemTemplate>
<asp:TextBox runat="server" id="myText" />
</ItemTemplate>
</ListView>
to get the text in myText, you can do this:
var text = ((TextBox) myListView.Items[0].FindControl("myText")).Text;
Related
I have a DataList control which is bound by SQLDataSource. The data source is fetching two column values, viz: ID, Name.
The DataList ItemTemplate looks like this:
<DataList>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval(Name) %>' />
</ItemTemplate>
</DataList>
I want the Label ID to be bound to the first column like:
<asp:Label ID='<%# Eval(ID) %> ...../>
But it gives error that ID should be simple.
Is there any way to use Eval value to generate assign to Label ID?
You can't assign it using data binding but you can access the ClientID and use that else were.
<label for='<%# Container.FindControl("TargetControlID").ClientID %>'>
Label Text
</label>
<asp:Label ID="TargetControlID" runat="server" Text="Target Control" />
I am displaying my data in a devexpress gridview. One of the column is state value. When the grid is edited, I have to show the state in a combobox, so that the user could change the state by choosing a different state. Currently it is displayed in a textbox, since it is the default. Essentially when the user clicks the edit button, a combobox should be displayed as part of the edit controls, and the combobox should be populated with all possible states in the codebehind and the selected value should be the initial value on the grid. It is very easy do it in MS gridview. But I couldn't see any sample code for how to do it in the devexpress gridview.
Thanks
Use GridViewDataComboBoxColumn. Declare datasource and attach it to combo box column or populate it in code behind. This example contains both variants.
You can also take a look at DevExpress grid editing demos.
<dx:GridViewDataTextColumn FieldName="FieldName" VisibleIndex="4">
<EditItemTemplate>
<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" DataSourceID="newDataSource" >
</dx:ASPxComboBox>
</EditItemTemplate>
</dx:GridViewDataTextColumn>
You'll need to set the datasource so you'll get the list of values
If you don't have a data source and want to include the combo box items in your code, here's another way to create the column:
<dx:GridViewDataComboBoxColumn FieldName="QAAproval" VisibleIndex="11" Width="30px">
<PropertiesComboBox>`enter code here`
<Items>
<dx:ListEditItem Text="GENERIC" Value="GENERIC" />
<dx:ListEditItem Text="FAIR" Value="FAIR" />
<dx:ListEditItem Text="VSE" Value="VSE" />
<dx:ListEditItem Text="ECAV" Value="ECAV" />
<dx:ListEditItem Text="FMMDS" Value="FMMDS" />
<dx:ListEditItem Text="CLEAR" Value="CLEAR" />
</Items>
</PropertiesComboBox>
<CellStyle Font-Size="XX-Small">
</CellStyle>
</dx:GridViewDataComboBoxColumn>
Edit the GridView Template, and in the EditTemplate of the field, add the dropdownbox. It might come to look like this
<dx:GridViewDataTextColumn Caption="Field Name"
FieldName="FieldName" VisibleIndex="3">
<EditItemTemplate>
<cc1:DropDownList ID="DropDownList1" runat="server">
</cc1:DropDownList>
</EditItemTemplate>
</dx:GridViewDataTextColumn>
So when you edit that row, it will show the DDL
I have used the following code to have combo box in the aspxgridview.
I hope this example helps :
<dx:GridViewDataComboBoxColumn FieldName="DatabaseFieldName" Settings-FilterMode="DisplayText"
Width="3%" VisibleIndex="3" Visible="True" Caption="Priority" Settings-AutoFilterCondition="Contains"
HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center" CellStyle-HorizontalAlign="Center"
CellStyle-VerticalAlign="Top">
<PropertiesComboBox ValueType="System.String" DataSourceID="objDataSourceID"
Width="200px" Height="25px" TextField="TextFieldName" ValueField="ValueFieldName"
IncrementalFilteringMode="StartsWith">
</PropertiesComboBox>
</dx:GridViewDataComboBoxColumn>
I'm trying to create a draggable list to change the order some pictures are displayed in a product page. I wan't to be able to do this in the product edit page (same place where I add the pics and their description). So, when creating I have nothing inserted on the database and since the AJAX toolkit reorderlist only works with a datasource I was specifying the list as the source of the reorderlist and calling the databind method. In the item template of the reorder list I have a textbox to edit the pic description and a img that displays the photo. I can drag the items and the list gets reordered, however when I click save I can't get the updated text on the textbox and the order property on the picture doesn't get updated. I've tried manually getting the items in the reorderlist but they're always null even though the list shows 20 items the DataItem is empty. I've enabled viewstate and didn't help either.
Here's my code:
<ajaxToolkit:ReorderList ID="rdlPhotos" runat="server" SortOrderField="PhotoOrder" AllowReorder="true" PostBackOnReorder="true" ClientIDMode="AutoID" EnableViewState="true" ViewStateMode="Enabled">
<ItemTemplate>
<div>
<%--<eva:PhotoView ID="iPV" runat="server" Photo='<%# Container.DataItem %>' />--%>
<asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# string.Format("http://eva-atelier.net/sparkle{0}", Eval("Path").ToString().Substring(1)) %>' Width="150" Height="150" />
<div class="formGrid">
<label class="formLabel">Title</label>
<asp:TextBox ID="txtTitle" runat="server" Text='<%#Bind("FileTitle") %>' />
<br />
<label class="formLabel">Description</label>
<asp:TextBox ID="txtDescription" runat="server" Text='<%#Bind("FileDescription") %>' />
<br />
</div>
<p>
<asp:Button ID="btnRemove" runat="server" Text="Remover" />
</p>
</div>
</ItemTemplate>
<ReorderTemplate>
<asp:Panel ID="pnlReorder" runat="server" />
</ReorderTemplate>
<DragHandleTemplate>
<div style="width:20px;height:20px;background-color:Red"></div>
</DragHandleTemplate>
</ajaxToolkit:ReorderList>
And below the C# code:
private void AddPhotosView()
{
if (_currentProduct.Photos != null && _currentProduct.Photos.Count > 0)
{
rdlPhotos.DataSource = _currentProduct.Photos;
rdlPhotos.DataBind();
}
}
I'm new to Asp.net I come from a large WPF background, sorry if I'm making basic question :)
Thanks
For updating order of ReorderList items add your handler for it's OnItemReorder event. In this case your handler may looks like this:
protected void ReorderHandler(object sender, ReorderListItemReorderEventArgs e)
{
var movedPhoto = _currentProduct.Photos[e.OldIndex];
_currentProduct.Photos.RemoveAt(e.OldIndex);
_currentProduct.Photos.Insert(e.NewIndex, movedPhoto);
_currentProduct.Photos.Save();
}
For updating FileTitle and FileDescription of single Photo it is easy to use OnUpdateCommand event of ReorderList and a button with attribute CommandName="Update" for each Photo.
And for updating all Photos at once just iterate through rdlPhotos.Items in next manner:
protected void SaveAllHandler(object sender, EventArgs e)
{
foreach (var riItem in rdlPhotos.Items)
{
var id = ((HiddenField)riItem.FindControl("itemID")).Value;
var title = ((TextBox)riItem.FindControl("txtTitle")).Text;
var description = ((TextBox)riItem.FindControl("txtDescription")).Text;
UpdatePhoto(id, title, description);
}
}
Remember that rdlPhotos.Items here are in initial order. And for identifying which Photo should be updated add hidden field with Photo.ID-value to ReorderList's ItemTemplate like this:
<asp:HiddenField runat="server" ID="itemID" Value='<%# Eval("ID") %>' />
I can't find any answer anywhere. I want to refer to the row object itself in an databinding expression in a gridview, like this:
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label runat="server"
Text = '<%# GetPendingReason(Eval("this")) %>' />
</ItemTemplate>
</asp:TemplateField>
But it doesn't work because "this" doesn't refer to any attribute. Referencing individual attributes works fine, but how do you refer to the current row?
Simply use <%# Container.DataItem %>. Do not use Databinder.
If you want to refer to the current row you do that at codebehind using
GridViewRow row = GridView1.Rows[index];
at any of the GridView event.
i'm loosing my mind. Using ASP.NET in a GridView, amongst other controls, I have the following:
<asp:TemplateField HeaderText="Intention">
<EditItemTemplate>
<asp:TextBox ID="IntentionInfo" Enabled="true" TextMode="MultiLine" Wrap="true" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="IntentionInfo" Enabled="false" TextMode="MultiLine" runat="server" />
</ItemTemplate>
</asp:TemplateField>
I would like to take the value out of this textarea and save in a database. However, server side, I try to pull the value out, like such:
string txt = (TextBox)DonationResultsTable.Rows[e.RowIndex].Cells[6].Controls[1].Text;
... but I keep getting the value that was SENT to the Client.
I wrote this javascript and I can see the values change in the DOM, but still the server keeps taking the old value.
$("textarea").change(function()
{
var txt = $(this).val();
$(this).html(txt).text(txt);
});
So my guess was ViewState, but I disabled it for those controls, like this:
<asp:TextBox ID="IntentionInfo" ViewStateMode="Disabled" Enabled="false" TextMode="MultiLine" runat="server" />
Still nothing! Any ideas?
One option could be to use a hidden field and update it on text changed for the text area. You could do this with jQuery like this:
$("textarea[id$=tbTest]").change(function () {
$("input[id$=hdnVal]").val($("textarea[id$=tbTest]").val());
});
Then on the server side, you can retrieve the hidden field's value and save it to your database.