Search using textbox value, then displaying it in gridview - asp.net

Now I'm trying to make a page that supposed to display the search result in the datagrid view, the page looks like this:
here's the stored procedure
alter PROCEDURE [dbo].[msProject_Select]
ProjectCode Varchar(50)
AS
BEGIN
SET NOCOUNT ON;
SELECT [projectCode]
,[projectName]
FROM Master..[MS_Project]
WHERE [projectCode] like '%' + #ProjectCode + '%'
ORDER BY [projectCode] ASC
END
the textbox in aspx page:
<td align="left" width="200px">
<asp:TextBox ID="TbProjectCode" runat="server" Width="194px"></asp:TextBox>
</td>
the search button in aspx page:
<td align="center" width="25px">
<asp:ImageButton ID="BtnSearch" runat="server" ImageUrl="../Support/Image/MagnifierGlass.png"
Width="75%" Height="75%" OnClientClick="openNewWin();return false;" />
</td>
and the datagrid in aspx page:
<td>
<asp:Panel ID="PanelDGV" runat="server" Height="100%" ScrollBars="None" Width="100%">
<asp:GridView ID="DGV" runat="server" AutoGenerateColumns="False" GridLines="None"
AllowPaging="true" PageSize="2" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:BoundField DataField="ProjectCode" HeaderText="Project Code" />
<asp:BoundField DataField="ProjectName" HeaderText="Project Name" />
<asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonField>
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
<AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
</asp:GridView>
</asp:Panel>
how can I supposed to do it? The idea is when I type what I want then click the button, the stored procedure will run and then the result will be display in the datagrid view, so far I try with no result, anyone can help? thank you.

You should have an event handler on the button that runs the stored procedure and binds the resulting data to the GridView.

Related

Bind Gridview Datasource with Linq (asp.net)

Basically, what I want to do is to bind a Linq query to the datasource of a gridview. Here is my code so far :
ddgDossiers.DataSource = (From c In dbConnection.Campaigns.AsQueryable Select c).ToList
ddgDossiers.DataBind()
It compiles and doesn't complain of anything.
<asp:GridView ID="ddgDossiers" runat="server" CellPadding="0" CellSpacing="0" AllowSorting="True"
AutoGenerateColumns="False" EnableViewState="True" AllowPaging="True" PageSize="35"
PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" PagerStyle-Position="TopAndBottom"
BorderWidth="1" GridLines="Both" BorderColor="#000000" CssClass="mGrid" PagerStyle-CssClass="pgr">
<PagerSettings Position="TopAndBottom" />
<Columns>
<asp:TemplateField Visible="false" HeaderText="UniqueID" ItemStyle-HorizontalAlign="left"
HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="1%" HeaderStyle-Width="1%"
HeaderStyle-CssClass="GridTitle" ItemStyle-CssClass="rowReport" SortExpression="UniqueId">
<ItemTemplate>
<asp:Label ID="lblUniqueID" runat="server" Text='<%#Container.DataItem("Campaign.CampaignId").ToString%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The error is pointing on this line :
<asp:Label ID="lblUniqueID" runat="server" Text='<%#Container.DataItem("CampaignId").ToString%>' />
And saying : No default member found for type 'Campaign'.
Is there anything to do with that? Thanks in advance.
change line
Text='<%#Container.DataItem("Campaign.CampaignId").ToString%>'
To
Text='<%# DataBinder.Eval(Container.DataItem, "CampaignId") %>'

Get Checked rows of a nested Gridview on button click

I have nested a Gridview2(child) and I want to get the checked rows on a button click.
When I try to access Gridview2 from button click event I am not able to do that. However, I can access the parent Gridview1.
Can somebody explain me how to get the Child Gridview's checked rows on button click.
Also, Button is a column header of child Gridview.
Here is my code.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Horizontal"
onrowdatabound="GridView1_RowDataBound" DataKeyNames="id1">
<AlternatingRowStyle BackColor="#F7F7F7" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:collapseExpand('id1_<%# Eval("id1") %>');">
<img id="imageSubId_<%# Eval("id1") %>" alt="Click to show/hide orders" border="0"
src="Images/bullet_toggle_plus.jpg" /></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id1" HeaderText="ID" />
<asp:TemplateField>
<ItemTemplate>
<tr>
<td colspan="100%">
<div id="rid1_<%# Eval("id1") %>" style="display: none; position: relative; left: 25px;">
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" CellPadding="4" ForeColor="#333333"
GridLines="None" OnRowCommand="Gridview2_RowCommand">
<Columns>
<asp:BoundField DataField="fname" HeaderText="First Name" />
<asp:BoundField DataField="mname" HeaderText="Middle Name" />
<asp:BoundField DataField="lname" HeaderText="Last Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkselect" runat="server" />
</ItemTemplate>
<HeaderTemplate>
<asp:Button ID="Button4" runat="server" Text="Remove" CommandName="Split" OnClick="Button4_Click" />
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server" TargetControlID="Button4" PopupControlID="Panel1" CancelControlID="Button1">
</ajaxToolkit:ModalPopupExtender>
</HeaderTemplate>
</asp:TemplateField> </Columns></asp:GridView>
</div>
</td>
</tr>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
What I'd do is add a CommandArgument to Button4's declaration that will allow me to find the related GridView2.
So, I'd use what you're using elsewhere, id1. Add CommandArgument='<%# Eval("id1") %>' to your Button4 declaration.
Now, in Button4_Click, you can cast the sender to a Button like so:
var button4 = (Button)sender;
Once you have button4 casted correctly to a Button, you can access the CommandArgument property.
var id1 = button4.CommandArgument;
Once you have id1, it's as simple as iterating through the parent GridView, GridView1. Looks like your second column is bound to id1 as well, so you'd do the following:
GridView foundGridView2;
foreach(GridViewRow gvr in GridView1.Rows)
{
if(gvr.RowType == DataControlRowType.DataRow && gvr.Cells[1].Text == id1)
{
foundGridView2 = gvr.FindControl("GridView2");
break; // Once we've found it, no need to iterate through other items
}
}
At this point, you now have access to your found GridView2, and you can iterate through the rows of GridView2 and check the checkboxes. Let me know if you need help doing that.
Adding to the answer because I think the parent GridView1 may be swallowing Button4's click:
Create an event handler for GridView1's OnRowCommand event.
In the resulting GridView1_RowCommand() method, use the following to handle JUST your Button4 Click event:
if(e.CommandName == "Split")
{
// At this point I would refactor out the code you put in Button4_Click
// into a separate method. I'll give you an example:
HandleButton4Click(e.CommandArgument); // e.CommandArgument will contain the id1
}

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.

How to make text aligin in Gridview?

In Asp.Net, how to apply text align [left,center,right] in a column, when autogenerate column is true. In every row of gridview the text is displayed in center of the column, i want to display in left side of the column. Thank you.
Try using below syntax if all the columns needs to left aligned
<RowStyle HorizontalAlign="Left"></RowStyle>
Also check these MSDN resource which has various examples for GridView formatting and Code Project article which shows examples for AutoGenerated Columns scenerio.
Sorry, this case will work only when autogeneratedcolumns = "false". See the edit for the autogeneratecolumns="true" If you want to align the header of the column add this to the BoundFiled:
HeaderStyle-HorizontalAlign="Right"
And if you want to align the item of the column add this to the BoundField:
ItemStyle-HorizontalAlign="Right"
EDIT:
Also try this too
Click on the gridView->properties->RowStyle:horizontal-align
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
ondatabound="CustomersGridView_DataBound"
runat="server">
<pagerstyle forecolor="Blue"
backcolor="LightBlue"/>
<pagertemplate>
<table width="100%">
<tr>
<td style="width:70%">
<asp:label id="MessageLabel"
forecolor="Blue"
text="Select a page:"
runat="server"/>
<asp:dropdownlist id="PageDropDownList"
autopostback="true"
onselectedindexchanged="PageDropDownList_SelectedIndexChanged"
runat="server"/>
</td>
<td style="width:70%; text-align:right">
<asp:label id="CurrentPageLabel"
forecolor="Blue"
runat="server"/>
</td>
</tr>
</table>
</pagertemplate>
</asp:gridview>
more info on this link
You can define <RowStyle> and <HeaderStyle> elements in your markup.
Example:
<asp:GridView ID="productGridView" Runat="server"
DataSourceID="productsDataSource"
DataKeyNames="ProductID" AutoGenerateColumns="False"
BorderWidth="1px" BackColor="#DEBA84"
CellPadding="3" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84">
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510"
HorizontalAlign="Center"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#A55129"></HeaderStyle>
</asp:GridView>
Put your columns as template fields as per below and give ItemStyle-HorizontalAlign="Left"
<Columns>
<asp:TemplateField ItemStyle-Width="5%" HeaderText="No." ItemStyle-
HorizontalAlign="Left">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
So it will work in all browser.

Resources