Attempting to set Default Value of TextBox1 within FormView - c#

ASP.Net using C# and WebForms
I am simply trying to populate a TextBox within a FormView with a value of approved when the page loads. The FormView's DEFAULT MODE is Edit and it will remain in Edit only. I am in need of some code and syntax that works. I have tried but I cant get the field to populate. I'm including the FormView markup and the Code Behind. I appreciate anyone that can help with this. I know it may be simple but I have had all kinds of trouble with it and I have not found anything that works.
Most of the solutions I have come across are for Inserting.
FormView1 Markup
<asp:FormView ID="FormView1" runat="server" DataKeyNames="id" DataSourceID="test_default" DefaultMode="Edit" AllowPaging="True" >
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" BackColor="#FFFFCC" Height="35px" Width="200px"></asp:TextBox>
<br />
id:
<asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' />
<br />
status:
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
if (FormView1.CurrentMode == FormViewMode.Edit)
{
TextBox TextBox2 = FormView1.FindControl("TextBox2") as TextBox;
if (TextBox2 != null)
{
TextBox2.Text = "approved";
}

This worked!
protected void Page_PreInit(object sender, EventArgs e)
{
if (submitted_to_email != null)
{
_ = Context.User.Identity.Name;
if (User.Identity.IsAuthenticated)
submitted_to_email.Text = User.Identity.Name;
}
if (TextBox1 != null)
{
TextBox1.Text = "approved";
}
}

Related

Display calculated value of 2 textbox values to third textbox

I have calculated the two textbox values and displayed successfully it into third textbox but the issues are when I enter value to textbox1 and move to textbox2 the page get reload as I have mentioned AutoPostBack="true" for textbox1 and textbox2 and when I remove this AutoPostBack="true" the calculated value is not displayed in third textbox.
Here is my code behind
protected void txttotal_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text))
txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString();
}
protected void txtdiscount_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txttotal.Text) && !string.IsNullOrEmpty(txtdiscount.Text))
txtgrandtotal.Text = (Convert.ToInt32(txttotal.Text) - Convert.ToInt32(txtdiscount.Text)).ToString();
}
Here is aspx code
<asp:TextBox ID="txttotal" runat="server" CssClass="textstyle" OnTextChanged="txttotal_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txtdiscount" runat="server" CssClass="textstyle" OnTextChanged="txtdiscount_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:TextBox ID="txtgrandtotal" runat="server" CssClass="textstyle" ReadOnly="true"></asp:TextBox>
To add the two text box value and show it on third text box you can add them on button click event here is the code
aspx page
<div>
<asp:TextBox runat="server" ID="txt1"></asp:TextBox>
<asp:TextBox runat="server" ID="txt2"></asp:TextBox>
<br />
<asp:Label runat="server" Text="Answer"></asp:Label> <asp:TextBox runat="server" ID="txt3"></asp:TextBox>
<asp:Button runat="server" ID="btn1" Text="CLICK TO ADD" OnClick="btn1_Click"/>
</div>
.cs
protected void btn1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txt1.Text) && !string.IsNullOrEmpty(txt2.Text))
{
txt3.Text = (Convert.ToInt32(txt1.Text) + Convert.ToInt32(txt2.Text)).ToString();
}
else {
Response.Write("Please Enter Value");
}
}

How to change the layout of QueryableFilterRepeater?

I am using an ASP.NET Dynamic Data Entities web application which contains a QueryableFilterRepeater server control inside the content placeholder on page List.aspx, when I execute the website, the filter control shows all the filters (Label Name along with the corresponding Drop Down) vertically.
Is there any way we can change the layout and display the filters horizontally?
Please find the .aspx code below
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged" />
</ItemTemplate>
</asp:QueryableFilterRepeater>
The corresponding .cs file code:-
protected void Label_PreRender(object sender, EventArgs e)
{
Label label = (Label)sender;
DynamicFilter dynamicFilter = (DynamicFilter)label.FindControl("DynamicFilter");
QueryableFilterUserControl fuc = dynamicFilter.FilterTemplate as QueryableFilterUserControl;
if (fuc != null && fuc.FilterControl != null)
{
label.AssociatedControlID = fuc.FilterControl.GetUniqueIDRelativeTo(label);
}
}
protected void DynamicFilter_FilterChanged(object sender, EventArgs e)
{
GridView1.PageIndex = 0;
}
Need suggestions.
Thanks in advance.
I usually wrap the control in a span element and then use CSS to style it the way that i want:
<asp:QueryableFilterRepeater runat="server" ID="FilterRepeater">
<ItemTemplate>
<span class="filter">
<asp:Label runat="server" Text='<%# Eval("DisplayName") %>' OnPreRender="Label_PreRender" />
<asp:DynamicFilter runat="server" ID="DynamicFilter" OnFilterChanged="DynamicFilter_FilterChanged"/>
</span>
</ItemTemplate>

How to find control in ListView on Page Load

I am trying to find the formview inside my listview on the page load. However, my result is always null. I called the DataBind method first but still nothing.
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
String list = itemdropdownlist.SelectedValue;
switch (list)
{
case "Section Item":
SectionListView.DataBind();
//SectionListView.Enabled = false;
var temp = (FormView)SectionListView.FindControl("SectionFormView");
temp.Enabled = true;
renderView(SectionListView, "hidden"); // hide listview on page load
break;
}
}
ASP.net code
<InsertItemTemplate>
<tr style="">
<td>
<div style="font-size: .8em;">
<asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
<ItemTemplate>
<asp:Button ID="InsertButton" runat="server" Text="Insert" OnClick="SectionItemButton_Click" Font-Size="1.2em" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
<asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemTextBox" runat="server" />
<asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemLabelTextBox" runat="server" />
</ItemTemplate>
</asp:FormView>
</div>
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
You got that error, because the listview was not binded yet, so i think the best way would be to do all this on the ItemDataBound event. You would find the FormView like:
if (e.Item.ItemType == ListViewItemType.DataItem)
{
FormView SFormView= (FormView)e.Item.FindControl("SectionFormView");
if (SFormView!= null)
{
//code here
}
}
You could use this code to find about anything. I used it before to find Literals that I created in code behind. Use this code to find your FormView
private readonly List<FormView> _foundControls = new List<FormView>();
public IEnumerable<FormView> FoundControls
{
get { return _foundControls; }
}
public void FindChildControlsRecursive(Control control)
{
foreach (Control childControl in control.Controls)
{
if (childControl.GetType() == typeof(FormView))
{
_foundControls.Add((FormView)childControl);
}
else
{
FindChildControlsRecursive(childControl);
}
}
}
FindChildControlsRecursive(<Insert relevent Code Here: Whatever element you want to search inside of like your listView, find that using FindControl>);
FormView[] strControl = new FormView[200];
strControl = FoundControls.ToArray();
foreach (FormView i in strControl)
{
if (i.ID.Equals("< insert controlId of your FormView>"))
{
// do something when you find it
}
}

how to get image url on click in repeater

i want to get image url on clicking the image from database in repeater
my database contains(id,url)
my repeater code is:
<asp:Repeater runat="server" ID="repeater" >
<ItemTemplate >
<asp:ImageButton runat="server" Width="200px" Height="200px" ImageUrl='<%#Eval("url") %>' OnCommand="Image_Click" CommandName="ImageClick" CommandArgument='<%#Eval("url") %>' />
</ItemTemplate>
</asp:Repeater>
my .cs code is
protected void Image_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == "ImageClick")
{
string a=e.CommandArgument.tostring();
responce.write(a);
}
}
you can do in ImageClick
((ImageButton)sender).ImageUrl
to get the url of the clicked button
Cast sender to ImageButton and read its ImageUrl property.
Also, is there a reason you are using command instead of handling ImageButtons click event?
Both Vladimir and Guigui answers are prefer way of accessing URL of an image button.
If you also want ID value in addition to URL, you can store multiple values into CommandArgument separated by comma.
<asp:ImageButton runat="server"
ImageUrl='<%#Eval("url") %>'
OnCommand="Image_Command"
CommandName="ImageClick"
CommandArgument='<%# Eval("Id")%> + "," + Eval("url") %>' />
protected void Image_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "ImageClick")
{
string[] commandArgs = e.CommandArgument.ToString()
.Split(',');
string id = commandArgs[0];
string url = commandArgs[1];
}
}

Handling control events from Repeater footer

Assuming I have the following repeater.
<asp:Repeater ID="MyRepeater" runat="server" onitemdatabound="MyRepeater_ItemDataBound">
<FooterTemplate>
</table>
<asp:Button ID="btnPrevious" runat="server" Text="<" />
<asp:Label ID="lblCurrentPage" runat="server" Text="<%# PagingStatus() %>" />
<asp:Button ID="btnNext" runat="server" Text=">" />
</FooterTemplate>
</asp:Repeater>
How can I handle the click events from btnPrevious and btnNext?
I have tried the following:
protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Button btnPrevious = (Button)e.Item.FindControl("btnPrevious");
Button btnNext = (Button)e.Item.FindControl("btnNext");
if (btnPrevious != null)
btnPrevious.Click += btnPrevious_Click;
if (btnNext != null)
btnNext.Click += btnNext_Click;
}
But this has failed (The event is never raised)..
You can use them in the same way you would use a normal button event handler eg:
Html:
<asp:Button ID="btnNext" runat="server" CommandArgument="<%=Id%>" onclick="Button_OnClick" Text=">" />
Code:
protected void Button_OnClick(object sender, EventArgs e)
{
Button button = sender as Button;
if(button != null)
{
string commandArg = button.CommandArgument;
//Do Work
}
}
The you can use the command argument to find out which button was clicked.
Hope this helps.
I would suggest using the ItemCommand event of the repeater. You still have to add the commands to your buttons though. Like this:
<asp:Button ID="btnPrevious" runat="server" Text="<" CommandName="Previous"/>
protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName.ToLower().Equals("previous")) {
//go back
}
else
{
//go forward
}
}

Categories

Resources