I want to keep my grid view hidden until and unless search button is been clicked
<td class="style2">
<asp:TextBox ID="txtbkcgry" runat="server" Width="233px"></asp:TextBox>
</td>
<td class="style3">
Auther</td>
<td class="style4">
<asp:TextBox ID="txtathr" runat="server" Width="235px"></asp:TextBox>
</td>
<td rowspan="2">
<asp:Button ID="Button1" runat="server" Text="Search" Width="143px"
onclick="Button1_Click" />
</td>
</tr>
<tr>
<td class="style1">
Book Name</td>
<td class="style2">
<asp:TextBox ID="txtbknm" runat="server" Width="232px"></asp:TextBox>
</td>
<td class="style3">
Price</td>
<td class="style4">
<asp:TextBox ID="txtprs" runat="server" Width="233px"></asp:TextBox>
Please guide me with the query.
This one
<asp:GridView ID="gridview1" runat="server" visible="false" ></asp:GridView>
In button event make it visible
protected void Button1_Click(object sender, EventArgs e)
{
gridview1.visible = true;
}
You can simply do this very well in client side.
Initially you can set the gridview style to display none in server side
protected void Page_Load(object sender, EventArgs e)
{
gridview1.Style.Add(HtmlTextWriterStyle.Display,"none");
//or//gridview1.Attributes.Add("style","display:none");
}
In a client side when a button is clicked
<asp:Button Text="Search" ID="txtSearch" runat="server"
OnClientClick="return showGridView()" />
In a javascript
function showGridView()
{
document.getElementByID("<%=gridView1.ClientID %>").style.display="block";
return false;
}
Note:
ASP.Net gridview will be rendered as HTML <table style="display:none" id="gridView1">
Please see the viewsource of a page and ensure that it added the display:none
specify a css class to your gridview where you say display: none, then change the css class of gridview to display block, or remove class to gridview
Related
I have created a table in my .aspx file that looks like this:
Here is the code that does this:
<!-- code for generating the "add selected sessions" button -->
<table>
<tr>
<td><strong>Individual Sessions</strong></td>
<td >
<div class="addButton" style="text-align: center;">
<asp:LinkButton ID="LinkButton2" runat="server" Text="Add Selected Sessions" OnClick="btnAddToCart_Click" />
</div>
</td>
</tr>
</table>
<!-- add all the sessions for the user to select -->
<asp:Repeater ID="rptFeesSession" runat="server">
<HeaderTemplate>
<table >
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hdnIsSession" runat="server" Value='<%#Eval("isSession")%>' />
<tr runat="server" visible='<%# Eval("isSession")%>'>
<td valign="top" colspan="2" style="position: relative;">
<asp:HyperLink CssClass="siteColorFG popBtn" ID="hlFeeType" runat="server" Text='<%#Eval("title")%>' NavigateUrl="javascript:;"/>
</td>
<td valign="top">
<div class="">
<asp:CheckBox ID="LinkButton3" CommandArgument='<%#Eval("id")%>'CssClass="checkB" OnClick="btnAddToCart_Click" runat="server" Text='<%#Eval("amount", "{0:C}")%>' />
</div>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
In my code behind file i want to capture all the checkboxes that have been checked and their respective CommandArgument values.
protected void btnAddToCart_Click(object sender, EventArgs e)
{
using (MyEntities db = new MyEntities())
{
//button was clicked. fetch all the check boxes from the rptFeesSession repeater into an int[]
}
}
There are several issues in your code (including conceptual / logic)
Item events in a Repeater should address item related things.
Click event handler has no access to CommandArgument attribute. Use Command instead.
Checkbox control doesn't support onclick event.
Checkbox events can run immediately only when there is AutoPostback="true".
If you want to refresh all repeater data on change of any checkbox then you can do something like this.
<asp:ScriptManager runat="server" ID="scriptMgr" /><%-- Strongly recommended --%>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Repeater ID="rptFeesSession" runat="server">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<asp:HiddenField ID="hdnIsSession" runat="server" Value='<%#Eval("isSession")%>' />
<tr runat="server" visible='<%# Eval("isSession")%>'>
<td colspan="2" style="position: relative;">
<asp:HyperLink CssClass="siteColorFG popBtn" ID="hlFeeType" runat="server" Text='<%#Eval("title")%>' NavigateUrl="javascript:;" />
</td>
<td>
<div class="">
<asp:HiddenField runat="server" ID="hidID" Value='<%#Eval("id") %>' />
<asp:CheckBox ID="LinkButton3"
AutoPostBack="true" CssClass="checkB"
OnCheckedChanged="LinkButton3_CheckedChanged" runat="server"
Text='<%#Eval("amount", "{0:C}")%>' />
</div>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
//.cs
protected void LinkButton3_CheckedChanged(object sender, EventArgs e)
{
decimal total = 0;
using (MyEntities db = new MyEntities())
{
foreach (RepeaterItem item in rptFeesSession.Items)
{
var chk = item.FindControl("LinkButton3") as CheckBox;
if(chk!=null && chk.Checked){
string id = (item.FindControl("hidID") as HiddenField).Value;
total += decimal.Parse(chk.Text);
//do stuff
}
}
}
}
On my form,I am having one drop down list.I have filled drop down list on page load event.Even i have check if not is post back.My drop down list's auto post back property is true.But still the event is not firing.I have to show some value in text box on selected index changed of drop down list.
my design code is as below:
enter code here
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<table class="tblContentcss">
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="lblUserRole" Text="Role"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:TextBox runat="server" ID="txtUserRole" Text="User" ReadOnly="true"></asp:TextBox>
</td>
</tr>
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="lblUserID" Text="User ID"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:DropDownList ID="ddlUserID" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlUserID_SelectedIndexChanged">
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="lblUserName" Text="User Name"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:TextBox runat="server" ID="txtUserName" AutoPostBack="true"></asp:TextBox>
</td>
</tr>
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="lblUserPassword" Text="Password"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td class="tdContent1css">
<asp:Label runat="server" ID="Label1" Text="Password"></asp:Label>
</td>
<td class="tdContent2css">
:
</td>
<td class="tdContent3css">
<asp:Button runat="server" ID="btnSave" Text="Save" OnClick="btnSave_Click" />
</td>
</tr>
</table>
</div>
</asp:Content>
and in.cs on page_load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillUserID();
}
}
protected void ddlUserID_SelectedIndexChanged(object sender, EventArgs e)
{
fillUserID();
SqlConnection con = dal.GetConnection();
dsUserName = new DataSet();
query = "SELECT CUSTID,(FNAME+' '+MNAME+' '+LNAME) AS USERNAME FROM CUSTOMER where CUSTID=" + Convert.ToInt32(ddlUserID.SelectedValue) + "";
dsUserName = dal.ReturnDataset(query, con);
if (dsUserName.Tables.Count > 0 && dsUserName.Tables[0].Rows.Count > 0)
{
txtUserName.Text = dsUserName.Tables[0].Rows[0]["USERNAME"].ToString();
}
}
what is the problem,i am not getting.
Suggest me any solution
When the autopostback is set True, after the page is rendered, a javascript is added on the onchange event of the dropdown (SELECT in HTML) to trigger the postback,
View the source after the page is rendered and check whether the javascript is getting added to the onchange event of the dropdown.
If you are adding some javascript on the onchange event of the dropdown in the code-behind, that might also stop the page to postback.
I get that from this link: https://www.codeproject.com/Questions/138183/Drop-down-selected-index-change-event-not-working
I have a ListView, and i have an asp:TextBox in it:
<table>
<asp:ListView ID="Users" runat="server"
DataKeyNames="Email" onitemediting="Users_ItemEditing"
onitemupdating="Users_ItemUpdating">
<LayoutTemplate>
<tr>
<td style=" width:34%;border-bottom:1px solid black;">Name</td>
<td></td>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name") %>
</td>
<td>
<asp:LinkButton ID="Edit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="Update" runat="server" CommandName="Update">Update</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
</asp:ListView>
</table>
when i change the text in the text box and then try to get it in the code behind like this:
protected void Users_ItemEditing(object sender, ListViewEditEventArgs e)
{
this.Users.EditIndex = e.NewEditIndex;
BindTheListView();
}
protected void Users_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
string Name=((TextBox)Users.Items[e.ItemIndex].FindControl("Name")).Text;
this.Users.EditIndex = -1;
BindTheListView();
}
*BindTheListView() Binds the listview Users, it works.
it takes the original text of the TextBox, the <%# Bind("name") %> instead of what i have just changed.
The update function is a lot longer so i put only what i think is needed. i know the Name recives the text that wast before in the textbox because the updtade function didnt work(it didnt change anything in the database) so i run debugg and i saw that Name recives what was before
How can i take the text that i have entered the textbox and not what was in it?
Thanks for the help
In User.ItemUpdating get new values from e.NewValues collection instead.
EDIT:
Use DataList instead of ListView:
<table>
<asp:DataList ID="Users" runat="server" DataKeyField="Email" OnEditCommand="Users_ItemEditing"
OnUpdateCommand="Users_ItemUpdating">
<HeaderTemplate>
<tr>
<td style="width: 34%; border-bottom: 1px solid black;">
Name
</td>
<td>
</td>
</tr>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name") %>
</td>
<td>
<asp:LinkButton ID="Edit" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="Name" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="Update" runat="server" CommandName="Update">Update</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
</asp:DataList>
</table>
Code behind:
protected void Users_ItemEditing(object sender, DataListCommandEventArgs e)
{
this.Users.EditItemIndex = e.Item.ItemIndex;
BindTheListView();
}
protected void Users_ItemUpdating(object sender, DataListCommandEventArgs e)
{
string Name = ((TextBox)e.Item.FindControl("Name")).Text;
}
on Page Load use
if (!IsPostBack)
{
BindTheListView();
}
I have a web feedback wizard and when I tested it in the browser, clicking the next button, it goes fine from step 1 to step 2 but then it refuses to go to step 3. Also, I tried to keep the focus on the first text box in each step, but it still isn't working right. when I click 'next' the step moves (as i said it works from Contact Info to Comments) but it scrolls up to the top of the web page and loses focus from the wizard. which is annoying.
so I have two problems: (1) my wizard only goes to two out of the four steps. (2) the wizard loses its focus.
here is what i have in my wizard control:
<asp:Wizard ID="Wizard1" runat="server" BackColor="#E6E2D8"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
Font-Names="Verdana" Font-Size="0.8em" ActiveStepIndex="0" Height="371px"
Width="691px" style="margin-top: 178px"
onfinishbuttonclick="Wizard1_FinishButtonClick" TabIndex="1"
onactivestepchanged="Wizard1_ActiveStepChanged"
onload="Wizard1_ActiveStepChanged" onnextbuttonclick="OnNextButtonClick"
onprerender="Wizard1_ActiveStepChanged" >
<HeaderStyle BackColor="#666666" BorderColor="#E6E2D8" BorderStyle="Solid"
BorderWidth="2px" Font-Bold="True" Font-Size="0.9em" ForeColor="White"
HorizontalAlign="Center" />
<NavigationButtonStyle BackColor="White" BorderColor="#C5BBAF"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="2em"
ForeColor="#1C5E55" />
<SideBarButtonStyle ForeColor="#855A21" />
<SideBarStyle BackColor="#E3D3AC" Font-Size="0.9em" VerticalAlign="Top" />
<StepStyle BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderStyle="Solid"
BorderWidth="2px" VerticalAlign="Top" />
<WizardSteps>
<asp:WizardStep ID="WizardStep1" runat="server" Title="Contact Info"
StepType="Start">
<table cellpadding="10" class="style1">
<tr>
<td class="style2">
Your Name</td>
<td class="style3">
<asp:TextBox ID="nametxt" runat="server" style="margin-left: 0px"
Width="137px"></asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="nametxt" ErrorMessage="Please Enter your name"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style5">
Email</td>
<td class="style6">
<asp:TextBox ID="emailtxt" runat="server"></asp:TextBox>
</td>
<td class="style6">
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="emailtxt" ErrorMessage="Please enter your email address"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style5">
City</td>
<td class="style6">
<asp:TextBox ID="citytxt" runat="server"></asp:TextBox>
</td>
<td class="style6">
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
ErrorMessage="Please enter your city or town"
ValidationExpression="^[a-zA-Z0-9\s.\-]+$" ControlToValidate="citytxt"></asp:RegularExpressionValidator>
</td>
</tr>
<tr>
<td class="style5">
State</td>
<td class="style6">
<asp:TextBox ID="statebox" runat="server" MaxLength="2" Width="47px"></asp:TextBox>
</td>
<td class="style6">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="statebox" ErrorMessage="Please enter your state"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td class="style3" colspan="2">
<asp:ValidationSummary ID="ValidationSummary1" runat="server" Width="163px" />
</td>
</tr>
</table>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep2" runat="server" Title="Comments"
StepType="Step">
<table cellpadding="10" class="style1">
<tr>
<td class="style4">
Comments<br />(no more than 500 characters)</td>
<td>
<asp:TextBox ID="txtcomments" runat="server" Height="55px" TextMode="MultiLine"
Width="233px"></asp:TextBox>
</td>
<td>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction="ValidateComments" ControlToValidate="txtcomments"
ErrorMessage="Please enter no more than 500 characters"
OnServerValidate="CustomerValidator1"></asp:CustomValidator>
</td>
</tr>
<tr>
<td class="style4">
Rating<br />(1-5)</td>
<td>
<asp:TextBox ID="txtrating" runat="server"></asp:TextBox>
</td>
<td>
<asp:RangeValidator ID="RangeValidator1" runat="server"
ControlToValidate="txtrating" ErrorMessage="Use a number between 1-5"
MaximumValue="5" MinimumValue="1"></asp:RangeValidator>
</td>
</tr>
<tr>
<td class="style4">
</td>
<td colspan="2">
<asp:ValidationSummary ID="ValidationSummary2" runat="server" Height="42px" />
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="txtcomments"></asp:CompareValidator>
</td>
</tr>
</table>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep3" runat="server" Title="Summary"
StepType="Finish">
<table cellpadding="10" class="style1">
<tr>
<td class="style7">
<h3>Summary:</h3>
<br />
<asp:Label ID="nameLabel" runat="server" AssociatedControlID="namelabel"></asp:Label>
</td>
<td class="style7">
</td>
</tr>
<tr>
<td>
<asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Emaillabel" ></asp:Label></td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="cityLabel" runat="server" AssociatedControlID="citylabel"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="stateLabel" runat="server" AssociatedControlID="statelabel"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="commentslabel" runat="server" AssociatedControlID="commentslabel"></asp:Label>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:Label ID="ratinglabel" runat="server" AssociatedControlID="txtrating"></asp:Label>
</td>
<td>
</td>
</tr>
</table>
</asp:WizardStep>
<asp:WizardStep ID="WizardStep4" runat="server" Title="Complete"
StepType="Complete">
<br />
<br />
<br />
<br />
<br />
<br />
<h3> Thank You!</h3>
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
here is what i have in the behind code: (c#)
protected void Page_Load(object sender, EventArgs e)
{
//Prevent display of sitemap on default page.
SiteMapNode node = SiteMap.CurrentNode;
if (node.ParentNode == null)
{
SiteMapPath1.Visible = false;
Wizard1.Visible = false;
}
nameLabel.Text = nametxt.Text;
EmailLabel.Text = emailtxt.Text;
cityLabel.Text = citytxt.Text;
stateLabel.Text = statebox.Text;
commentslabel.Text = txtcomments.Text;
ratinglabel.Text = txtrating.Text;
}
public void OnNextButtonClick(object sender, WizardNavigationEventArgs e)
{
this.Wizard1.Focus();
Wizard1_ActiveStepChanged(sender, e);
}
private TextBox FindFirstTextBox(Control c)
{
TextBox nothing = null;
Control results;
if ((c == null))
{
return nothing;
}
if (c.GetType() == typeof(TextBox))
{
return (TextBox)c;
}
foreach (Control child in c.Controls)
{
results = FindFirstTextBox(child);
if (results != null && (results.GetType() == typeof(TextBox)))
{
return (TextBox)results;
}
else
{
return nothing;
}
}
return nothing;
}
protected void Wizard1_ActiveStepChanged(object sender, System.EventArgs e)
{
// Set the focus to the first TextBox in the current step
WizardStepBase currentWizardStep = Wizard1.ActiveStep;
// Find the first TextBox
TextBox firstTextBox = FindFirstTextBox(currentWizardStep);
// If we found a TextBox, set the Focus
if (!(firstTextBox == null))
{
firstTextBox.Focus();
}
}
Ok that is because... you have a compare validator on your txtComments field in the 2nd step of your wizard...I would recommend to either fix it or remove it...here is the code that I am referring to (in your Wizard Step 2)
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="txtcomments"></asp:CompareValidator>
The things that are missing in the above code are:
Set "ControlToCompare" property.
Set an error message...
Update for your second problem
To keep focus on your first text box...of each tab and to keep scroll position...follow below steps...
Step 1 In your Page_Load event...add the below line of code
nametxt.Focus(); // This will make sure that your nametxt texbox has focus when your page loads for the first time
Step 2 Your FindFirstTextBox method's code can be as simple as below...so update it to the code below...
// This code is pretty much self explanatory
private TextBox FindFirstTextBox(Control c)
{
foreach (Control child in c.Controls)
{
if (child is TextBox)
return (TextBox)child;
}
//If we didn't find a TextBox
return null;
}
Step 3 Update your Wizard_ActiveStepChanged event to below...
protected void Wizard1_ActiveStepChanged(object sender, System.EventArgs e)
{
// Set the focus to the first TextBox in the current step
WizardStepBase currentWizardStep = Wizard1.ActiveStep;
// Find the first TextBox
TextBox firstTextBox = FindFirstTextBox(currentWizardStep);
// If we found a TextBox, set the Focus
if (Page.IsPostBack && firstTextBox != null)
{
firstTextBox.Focus();
}
}
In the above event code, I have changed the if statement to have an additional check for page.IsPostBack...this is because the Focus method will throw an exception...since this event is being called before your OnLoad and OnRender...Its pretty much up to you to figure out why you are calling this step so early...
Step 4 Finally, to fix your scrolling problem...you need to use an Update panel...please see below code snippet...
// Your Update Panel needs a Script Manager to work
<asp:scriptManager runat="server" ID="sm"></asp:scriptManager>
<asp:UpdatePanel runat="server" ID="panel" UpdateMode="Always">
<ContentTemplate>
// ALL YOUR CONTENT MARK UP GOES HERE
</ContentTemplate>
</asp:UpdatePanel>
This should probably fix your second problem...
I have the following markup:
<tr>
<td valign="top" align="left">
<asp:Label ID="Label1" runat="server" Text="Available Roles" />
<br />
<asp:ListBox ID="availableRolesListBox" runat="server" SelectionMode="Multiple" Width="100px" Rows="10" AutoPostBack="false" />
</td>
<td valign="top" align="center">
<br />
<asp:Button ID="addToRole" runat="server" Text="--->" OnClick="addToRole_Click" />
<br />
<asp:Button ID="removeFromRole" runat="server" Text="<---" OnClick="removeFromRole_Click" />
</td>
<td valign="top" align="left">
<asp:Label ID="Label2" runat="server" Text="User In Roles" />
<br />
<asp:ListBox ID="userInRolesListBox" runat="server" SelectionMode="Multiple" Width="100px" Rows="10" AutoPostBack="false" />
</td>
</tr>
And the following in code-behind:
protected void addToRole_Click(object sender, EventArgs e)
{
// Add user to the selected role...
foreach (ListItem myItem in availableRolesListBox.Items)
{
if (myItem.Selected)
{
Roles.AddUserToRole(userListBox.SelectedItem.Value, myItem.Text);
}
}
Refresh();
}
When I step into the code-behind absolutely no items are selected! What am I forgetting?
Are you perhaps rebinding the availableRolesListBox each time, instead of if(!IsPostback)?
You could check a few things.
CHeck that you are NOT reloading the listbox after each postback. Also, you might want to make sure you do not have ViewStateEnabled="false" for a parent container.
Other than that your code looks like it should be ok, debugging any further would require more code or information.