Get control instance in asp.net dynamic data - asp.net

I am creating a web application using Asp.net dynamic data. I am using GridView to show data from the database.
In the grid view I am having following code for columns
<Columns>
<asp:DynamicField DataField="UserId" UIHint="Label" />
<asp:DynamicField DataField="Address" UIHint="Address"/>
<asp:DynamicField DataField="CreatedDate" UIHint="Label" />
</Columns>
But, before displaying I want to do some processing in C# code for each row. In normal ASP.net grid view we can handle OnRowDataBound method, and using FindControl("controlid") we can get the control instance, but in case of dynamic data, I am not getting any id attribute for columns, so I am not able to get the control instance to show updated data in that control depending on some conditions.
Thanks,
Ashwani

Try using a TemplateField instead of DynamicField: http://msdn.microsoft.com/en-us/library/bb288032.aspx
An example of how to use:
<asp:Templatefield headertext="Author Name">
<Itemtemplate>
<asp:label id="FirstNameLabel"
Text= '<%# Eval("au_fname") %>'
runat="server"/>
<asp:label id="LastNameLabel"
Text= '<%# Eval("au_lname") %>'
runat="server"/>
</Itemtemplate>
</asp:Templatefield>
Here are the options available to you:
<asp:TemplateField
AccessibleHeaderText="string"
ConvertEmptyStringToNull="True|False"
FooterText="string"
HeaderImageUrl="uri"
HeaderText="string"
InsertVisible="True|False"
ShowHeader="True|False"
SortExpression="string"
Visible="True|False">
<ControlStyle />
<FooterStyle />
<HeaderStyle />
<ItemStyle />
<AlternatingItemTemplate>
<!-- child controls -->
</AlternatingItemTemplate>
<EditItemTemplate>
<!-- child controls -->
</EditItemTemplate>
<FooterTemplate>
<!-- child controls -->
</FooterTemplate>
<HeaderTemplate>
<!-- child controls -->
</HeaderTemplate>
<InsertItemTemplate>
<!-- child controls -->
</InsertItemTemplate>
<ItemTemplate>
<!-- child controls -->
</ItemTemplate>
</asp:TemplateField>

Related

How to add a button control dynamically into a GridView cell ? VB.NET

Look, it´s simple. I have a GridView which is populated with data from my database.
What I want is put a button in each cell that contains a specific datum.
Pls, look the image below. It describes exactly what I want
In VB.NET, pls! =)
Example Image
Thanks very much!
You are going to need to define the button control inside of the <Columns> section of your GridView markup, like this:
<asp:gridview id="CustomersGridView" runat="server">
<columns>
<asp:boundfield datafield="DateColumn" headertext="Date"/>
<asp:TemplateField>
<HeaderTemplate>
Positive
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelPositive" runat="server" Text='<%# Eval("PositiveColumn")%>' />
<br />
<asp:Button id="ButtonPositive" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Negative
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelNegative" runat="server" Text='<%# Eval("NegativeColumn")%>' />
<br />
<asp:Button id="ButtonNegative" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Neutral
</HeaderTemplate>
<ItemTemplate>
<asp:Label id="LabelNeutral" runat="server" Text='<%# Eval("NeutralColumn")%>' />
<br />
<asp:Button id="ButtonNeutral" runat="server" Text="Show" />
</ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield="NoCommentsColumn" headertext="No Comments"/>
<asp:boundfield datafield="TotalColumn" headertext="Total"/>
</columns>
</asp:gridview>
Note: The datafield and Eval() calls are bound to made up names like NeutralColumn and NoCommentsColumn, substitute those names with your real database field names.
You need to add while data binding the grid. Look for an event call and handle your code there.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

Hide or insert tbody and tr tag in hidden gridview?

I'm trying to validate a page on our ASP.Net site that the developers used a gridview on. It seems that initially they are using a "placeholder" gridview that is getting hidden or visible based on some buttons being clicked to show data. Before these button clicks however, I'm seeing an empty table and the validation tool I'm using is complaining about no tbody or tr tags. Is there a way to completely hide the table tag or alternately to insert a tbody/tr within the hidden gridview?
Here is the gridview tag in the aspx.vb file:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" summery="This table displays system notification items."
AllowPaging="True" PagerSettings-Position="Top" PagerStyle-HorizontalAlign ="Right" PageSize ="15" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="image1" runat="server" ImageUrl="~/cmsicons/flag_red.gif" AlternateText="Overdue" Visible='<%# Eval("IsVisible")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataFormatString="{0:MM/dd/yyyy}" DataField="showdate" HeaderText="Due Date"
SortExpression="showdate" />
<asp:BoundField DataFormatString="{0:MM/dd/yyyy}" DataField="ModifiedDate" HeaderText="ModifiedDate"
SortExpression="ModifiedDate" />
<asp:TemplateField HeaderText="Data ID" SortExpression="DataRecordID">
<ItemTemplate>
<asp:HyperLink text='<%# Eval("DataRecordID") %>' runat="server" NavigateUrl='<%# Eval("DataRecordID", "CMSManageAllDataRecord.aspx?dataid={0}") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="RecordName" HeaderText="Title"
SortExpression="RecordName" />
<asp:BoundField DataField="subcdrl" HeaderText="CDRL Number"
SortExpression="subcdrl" />
<asp:BoundField DataField="subcdrl" HeaderText="Sub Cdrl Count"
SortExpression="subcdrl" Visible ="false" />
<asp:BoundField DataField="StatusName" HeaderText="Status"
SortExpression="StatusName" />
</Columns>
<EmptyDataTemplate>
<div>No Data found</div>
</EmptyDataTemplate>
<FooterStyle CssClass="GridViewFooterStyle" />
<RowStyle CssClass="GridViewRowStyle" />
<SelectedRowStyle CssClass="GridViewSelectedRowStyle" />
<AlternatingRowStyle CssClass ="GridViewAlternatingRowStyle" />
<PagerStyle CssClass="GridViewPagerStyle" HorizontalAlign="Right" />
<HeaderStyle CssClass="GridViewHeaderStyle" />
</asp:GridView>
And here is the output I see when I view source:
<div class="AspNet-GridView" id="ctl00_ContentPlaceHolder1_GridView1">
<table cellpadding="0" cellspacing="0" summary="">
</table>
</div>
You can definitely hide it with Javascript, if that's an option, one way would be
<script>
function hideGV(){
document.getElementById('<%=GridView1.ClientID%>').style.display='none';
}
</script>
If rather want to insert the tbody/tr elements and using jQuery is an option for you, here's an example.
jQuery option to insert tbody/tr
<script>
​$(function(){
$('#<%=GridView1.ClientID%>').append('<tbody><tr><td></td></tr></tbody>');
});​
</script>

Call a clientsideevent from another clientsideevent

ASP.net(C#), VS2010, Win 7.
New to WebDev, so this might be a simple syntax thing but here it goes...
Long story short I have to force a postback on a GridView that shows attachments I upload using an ASPxUploadControl. I put the GridView inside an ASPxCallbackPanel and am trying to get my GridView to update on the page after an attachment is uploaded using ASPxCallbackPanel.PerformCallback();
Here's the Upload control, in its' ClientSideEvent is where I'm trying to call the clientSideEvent from the Button shown below. Just trying to force a button click really, but I tried doing it from the code-behind but that didn't work. Any help would be appreciated!
<dxuc:ASPxUploadControl ID="FileUpload1" runat="server"
ClientInstanceName="uploader"
ShowAddRemoveButtons="False"
ShowUploadButton="True"
AddUploadButtonsHorizontalPosition="Center"
AddUploadButtonsVerticalPosition="Top" FileInputCount="1"
UploadMode="Advanced"
OnFileUploadComplete="UploadControl_FileUploadComplete"
Size="30">
<ClientSideEvents FileUploadComplete="function(s, e) { Button1.Click; }" />
<%-- <AdvancedModeSettings EnableMultiSelect="True" /> "does not have public property named "advancedModeSettings" version is too old--%>
<ValidationSettings
AllowedFileExtensions=".doc,.pdf,.xls,.txt,.jpeg,.jpg,.gif,.png,.oft,.htm,.html,.mht,.rtf,.zip"
MaxFileSize="5242880"
FileDoesNotExistErrorText="This file can't be found."
GeneralErrorText="Custom file uploading fails due to an external error that doesn't relate to the ASPxUploadControl's functionality"
MaxFileSizeErrorText="Size of the uploaded file exceeds maximum file size">
<ErrorStyle ForeColor="Red"/>
</ValidationSettings>
</dxuc:ASPxUploadControl>
Here is the GridView as well as a button I made that successfully refreshes the grid.
<div>
<dxe:ASPxButton ID="ASPxButton1" runat="server" ClientInstanceName="Button1" Text="Reload Panel" AutoPostBack="False">
<ClientSideEvents Click="function(s, e) {ASPxCallbackPanel1.PerformCallback(); e.processOnServer = true;}" />
</dxe:ASPxButton>
<dx:ASPxCallbackPanel ID="ASPxCallbackPanel1" runat="server"
ClientInstanceName="ASPxCallbackPanel1" Width="492px"
Height="100%">
<PanelCollection>
<dx:PanelContent runat="server">
<asp:GridView ID="gvAttachment" SkinID="grid" runat="server" Width="98%"
OnRowDataBound="AttachmentControl_OnRowDataBound"
meta:resourcekey="gvAttachResource1"
PagerSettings-FirstPageText="<%$ Resources:CommonControlText,FirstPageText %>"
PagerSettings-LastPageText="<%$ Resources:CommonControlText,LastPageText %>"
PagerSettings-PreviousPageText="<%$ Resources:CommonControlText,PreviousPageText %>"
PagerSettings-NextPageText="<%$ Resources:CommonControlText,NextPageText %>"
AutoGenerateColumns="False">
<EmptyDataRowStyle CssClass="emptyData" />
<EmptyDataTemplate>
<table class="usercontroldetail container_table">
<tr>
<td class="tdlayout">
<asp:Label ID="Localize1" runat="server">
<%= Placeholder %>
</asp:Label>
</td>
</tr>
</table>
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="AtchmtId" HeaderText="Attachment ID"
visible = "false" meta:resourcekey="BoundFieldResource1">
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField ="FileNm" HeaderText="File Name" />
<asp:BoundField DataField="UsrNm" HeaderText="Uploaded By"
/>
<asp:BoundField DataField="AtchmtDt" HeaderText="Date"
/>
<asp:BoundField DataField="FileSizeCnt" HeaderText="File Size"
/>
<asp:TemplateField AccessibleHeaderText="Actions" HeaderText="Actions">
<ItemTemplate>
<div style="text-align:center;">
<asp:LinkButton ID="btnDelete" Visible="False" runat="server"
ToolTip="Delete Selected Attachment"
OnClick="btnDelete_Click"
Text="Delete" CausesValidation="True" DisableOnSubmit="True" Group=""
meta:resourcekey="btnDeleteResource1" />
<asp:Label ID="lblPipe" runat="server" Text="|" />
<asp:LinkButton ID="btnView" Visible="False" runat="server"
ToolTip="View Selected Attachment"
OnClick="btnView_Click"
Text="View" CausesValidation="True" DisableOnSubmit="True" Group=""
meta:resourcekey="btnViewResource1" />
</div>
</ItemTemplate>
<ItemStyle Wrap="false" />
</asp:TemplateField>
</Columns>
<%--<PagerSettings FirstPageText="First" LastPageText="Last" NextPageText="Next >" PreviousPageText="< Previous"></PagerSettings>--%>
<RowStyle CssClass="row_odd" />
<AlternatingRowStyle CssClass="row_even" />
</asp:GridView>
</dx:PanelContent>
</PanelCollection>
</dx:ASPxCallbackPanel>
FYI: The GridView is getting the new attachment put in it. I'm just not seeing it on the page because the UploadControl only updates itself.
Edit:Figured it out just used the OnClick() method as below. Derp. Forgot that the ASPx button inherited all the ASP buttons methods. But now my original plan of making the button invisible seems to be foiled. When Button1.OnClick() is called with the Visible property for the Button set to "false". it says Button1 is not defined. Anyway around this?
<ClientSideEvents FileUploadComplete="function(s, e) { Button1.OnClick(); }" />
If I got you right, you can manipulate the behavior of all update panels on page via property called a updateMode (Conditional, Always). Changing one updatepanel will cause refreshing of all, if all have mode set to Always.

Hiding a cell with a Hidden Field

In my GridView, I am using a Hidden Field to store some data that is not supposed to be seen by the user:
<Columns>
<asp:BoundField DataField="Название" HeaderText="Название" ItemStyle-Width="250px" HeaderStyle-Width="250px" />
<asp:BoundField DataField="RDName" HeaderText="РД" ItemStyle-Width="250px" HeaderStyle-Width="250px" />
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="RD_ID" runat="server" Value='<%# Eval("RD_ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
I have a problem with this. Though the data is not seen in the GridView, the additional empty cell is still there. Could you please tell me how I can hide it completely?
Thanks,
David
Can't you do it like this?
<Columns>
<asp:BoundField DataField="Название" HeaderText="Название" ItemStyle-Width="250px" HeaderStyle-Width="250px" />
<asp:TemplateField HeaderText="РД" ItemStyle-Width="250px" HeaderStyle-Width="250px">
<ItemTemplate>
<asp:Label ID="RD_Name" runat="server" Text='<%# Eval("RDName") %>' />
<asp:HiddenField ID="RD_ID" runat="server" Value='<%# Eval("RD_ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
You can always put the hidden element alongside with any TemplateField ItemTemplate object. You don't have to create a cell to contain the hidden element.

asp.net gridview: How can I have multiple button fields in one column?

I need to create multiple actions for a gridview - say, "Approve", "Deny", and "Return".
I can do this by creating a button field for each action:
<asp:ButtonField ButtonType="Link" CommandName="Approve" Text="Approve" />
<asp:ButtonField ButtonType="Link" CommandName="Deny" Text="Deny />
<asp:ButtonField ButtonType="Link" CommandName="Return" Text="Deny" />
However, this creates one column for each button.
Is there a way to have the same functionality, but combine them into a single column?
Have you considered using an TemplateField? Here is an example:
<asp:GridView ID="grdTest" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnApprove" runat="server" CommandName="Approve" Text="Approve" />
<asp:LinkButton ID="btnDeny" runat="server" CommandName="Deny" Text="Deny" />
<asp:LinkButton ID="btnReturn" runat="server" CommandName="Return" Text="Return" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can then capture the commands the exact same way using OnRowCommand or do whatever you like. You have full control to make it behave how you need and not be bound by the built in functionality of using the regular predefined column types.
Try to put buttons into the <asp:TemplateField> instead:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CommandName="Approve" Text="Approve" />
<asp:LinkButton CommandName="Deny" Text="Deny />
<asp:LinkButton CommandName="Return" Text="Deny" />
</ItemTemplate>
</asp:TemplateField>
The solution is not to use ButtonField elements.
To accomplish what you want you will need to create a column as a TemplateField and define buttons as regular ASP.NET <asp:Button id="myButton" /> within TemplateField's ItemTemplate or EditItemTemplate as appropriate for your UI.
You can handle Click events in the GridView_OnItemCommand() handler, where you can check e.CommandName to figure out exactly which button caused the event.

Resources