Here is what I can see when I do e.Item.FindControl for the TextBox I'm trying to get the value for
I have 2 date inputs in a list view and I am trying to retrieve their values but always get an empty string.
If I set the value in the front end then it comes through to the code behind but if I edit the field the original value is returned.
I am able to get the values from and controls without an issue.
I've tried using asp:TextBox and html
<asp:TextBox ID="editArrivalFrom" name="editArrivalFrom" TextMode="Date" runat="server" OnTextChanged="editArrivalFrom_TextChanged"></asp:TextBox>
<input type="date" id="editArrivalTo" name="editArrivalTo" runat="server" />
var arrivalFrom = (TextBox) e.Item.FindControl("editArrivalFrom");
if (!String.IsNullOrWhiteSpace(arrivalFrom.Text))
var arrivalTo = (HtmlInputGenericControl) e.Item.FindControl("editArrivalTo");
if (!String.IsNullOrWhiteSpace(arrivalTo.Value))
I've also tried using Request.Form, all of the below return null:
var test = Request.Form[arrivalFrom.UniqueID];
var test2 = Request.Form[arrivalFrom.ClientID];
var test3 = Request.Form["editArrivalFrom"];
There is also no sign of either of the inputs in Request.Form.AllKeys
The asp control doesn't show up in the designer.cs file and adding it only results in it being null anyway.
Any suggestions would be much appreciated I've been pulling my hair out for hours.
Relevant code for the form update is below. Please note that all of this is contained within an update panel
<asp:ListView ID="MarkupRuleList" runat="server"
OnItemCommand="MarkupRuleList_OnItemCommand">
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" CssClass="lstview_record_bttn button-spacing-7" Text="Update" CommandName="Update" CommandArgument='<%# Eval("Id") %>' runat="server" />
</EditItemTemplate
protected void MarkupRuleList_OnItemCommand(object sender, ListViewCommandEventArgs e)
{
int markupId = 0;
int.TryParse(e.CommandArgument.ToString(), out markupId);
var markup = new MarkupRuleDTO();
switch (e.CommandName)
{
case "Update":
// GetMarkupRuleFromPage contains all of the code from the top of this post
markup = GetMarkupRuleFromPage(e, markupId);
break;
}
}
Edit: Solved by ditching the date input and using regular textbox, then performing the following onkeyup JS function
function dateInputHandler(item, event) {
// key code 8 == backspace
if (event.keyCode != 8) {
// insert forward slashes after days and months
if (item.value.length == 2 || item.value.length == 5) {
item.value = item.value + '/'
}
// remove double slashes and prevent length exceeding 10 characters
// format should be dd/mm/yyyy
if (item.value.endsWith("//") || item.value.length > 10) {
item.value = item.value.slice(0, -1);
}
}
}
You may be better off casting the sender object rather than the EventArgs object.
var x = ((TextBox)sender).Text;
But you can access the control directly (or should be able to).
var x = editArrivalFrom.Text;
And to get from the form, use the correct variable name:
var x = Request.Form[editArrivalFrom.ClientID];
Related
In a GridView I have the list of 2 bed and breakfasts (ShowAllBeb.aspx) which are in DB, one with idname = 0 and the other with idname = 1. I've created LinkButtons that will refer to the B&B detail (DisplayBeb.aspx)
Code aspx:
<asp:LinkButton ID="Label4" runat="server" Text='<%# Eval("name") %>'
ForeColor="#0066cc"
PostBackUrl='<%# "~/site/DisplayBeb.aspx?idname={0}" + Eval("name") %>'>
</asp:LinkButton>
code aspx.cs:
using (dcDataContext dc = new dcDataContext())
{
DataListBeb.DataSource = from beb in dc.beb
select beb;
string fullname1 = Request.QueryString["idname"];
DataListBeb.DataBind();
With this code, if I click on all 2 B & Bs, the detail page of the idname = 0 comes out for both of them. How can I make the other detail page of the B&B visible?
It looks like the query string itself is correct, so you're focusing on the wrong place for the problem. You are successfully getting the query string value:
string fullname1 = Request.QueryString["idname"];
You're just not using that value for anything. I suspect you probably want to use it in a where clause on your query:
string fullname1 = Request.QueryString["idname"];
DataListBeb.DataSource = from beb in dc.beb
where beb.name == fullname1
select beb;
or:
string fullname1 = Request.QueryString["idname"];
DataListBeb.DataSource = dc.beb.Where(b => b.name == fullname1);
Of course I don't know what your beb model looks like or what its properties are called, so beb.name was entirely a guess based on the use of Eval("name") above. Simply use whatever property is appropriate in your query.
<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound">
<HeaderTemplate>
<ul class="sf_newsList">
</HeaderTemplate>
<ItemTemplate>
<asp:Literal ID="Publication_Date" runat="server" Text="{0}" />
...
Ok so i have a date coming back from the source, i need to strip this date into day and month as it currently
comes back like "march 12 2012".
once parsed into a day and a month string i want to then pass it back into
<asp:Literal ID="Publication_Day" runat="server" Text="{0}" />
now ive tried some things such as
protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
string newsdate = "";
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Literal ltDate = (Literal)e.Item.FindControl("Publication_Date");
newsdate = ltDate.Text;
//do parse code
Literal ltDay = (Literal)e.Item.FindControl("Publication_Day");
ltDay.Text = newsdate;
}
}
but when i step through the publication_date is only returning {0} as a string not the date.
i know the source is fine, as if i just put the date on the template is shows up on the front end.
Any ideas what im doing wrong?
It is returnin {0} because it is on the Text property of Literal.
You could use the DataBinder.Eval to read the column of your DataSource from e.Item.DateItem. Since you know what is the column you want to read you could get the DateTime object direct from datasource, for sample:
// get the dateTime from datasource
DateTime datePublication = DateTime.Parse(DataBinder.Eval(e.Item.DataItem, "DatePublicationColumn").ToString());
// show the day name
ltDay.Text = datePublication.ToString("dddd");
Where I add DatePublicationColumn, you specify the column.
I am using asp.net webform 4.5.1 code first with entity framework. I used one repeater and bind it to my entity class. I want use if statement to decide show one DIV in this repeater or not. my code is :
<asp:Repeater ID="ProductRepeater" runat="server"
ItemType="Models.Product"
SelectMethod="ProductRepeate_GetData">
<ItemTemplate>
<% if(Item.Rank > 5 && Item.X != null && Item.Y != null){%>
<div>I want show this div just if my if statement is True</div>
<%}%>
<div >
<%# Item.Name%>
</div>
</ItemTemplate>
</asp:Repeater>
I want show the first div just when the result of if statement is True. the error is : The name 'Item' does not exist in the current context
This isn't the kind of calculation that you would want to include inline; not only will it be very difficult to read, it will also be very difficult to debug.
Instead, create a label <asp:Label ID="outputLabel" runat="server" ></asp:Label> and set the value of the label from the ItemDataBound Event on the repeater.
protected void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
Label output = (Label)item.FindControl("outputLabel");
Product product = (Product)item.DataItem;
if (product.Rank > 5 && product.X != null && product.Y != null)
{
output = "I want show this div just if my if statement is True";
}
else
{
output = product.Name;
}
}
I know you already got an answer, you can do it in markup also like this:
<%# (Item.Rank > 5 && Item.X != null && Item.Y != null)? "<div>I want show this div just if my if statement is True</div>" : "" %>
I am new to linq. I am using linq to get vlaues from datalist control and creating a datatable with that values. The datatable contains three columns. In that, sometimes the 'Answer' column is having a data which is comma separated.
DataTable SaveAnswer = clsSource.SetData();
var df = from DataListItem dli in PollDataList.Items
let z = ((HiddenField)dli.FindControl("IDReqHiddenField"))
let y = ((RadioButtonList)dli.FindControl("rdblstPollOptions"))
let v = ((CheckBoxList)dli.FindControl("CheckBoxListMultiplePollOptions"))
select new
{
PollId=z.Value,
Answered1 = y.SelectedValue,
Answered2=v.Items.Cast<ListItem>().Where(r=>r.Selected)
};
var result = from p in df
select SavePollAnswer.LoadDataRow(
new object[] {
p.PollId,p.Answered1+string.Join( ", ", p.Answered2 ),""
},
false);
SavePollAnswer = result.CopyToDataTable();
And here is my design
<asp:DataList ID="PollDataList" runat="server" CssClass="poll-preview">
<ItemTemplate>
<asp:HiddenField ID="PollIDReqHiddenField" Value='<%# Eval("PollID") %>' runat="server" Visible="false" />
<asp:Label ID="lblReqQuestionNumber" runat="server" Text='<%# Eval("NoofPollQuestion") %>' Font-Bold="true"></asp:Label><br />
<asp:Label ID="lblRequiredPollQusetion" runat="server" Text='<%# Eval("PollQuestions") %>' Font-Bold="false"></asp:Label>
<asp:Label ID="lblReqNotification" runat="server" ForeColor="Red" CssClass='<%# Eval("PollReq") %>' Text="*" Font-Bold="true"></asp:Label>
<asp:RadioButtonList ID="rdblstPollOptions" DataSource='<%# Eval("PollOptionsReq") %>' runat="server"></asp:RadioButtonList>
<asp:CheckBoxList ID="CheckBoxListMultiplePollOptions" DataSource='<%# Eval("PollOptionsMul") %>' runat="server" RepeatDirection="Vertical" RepeatColumns="1"></asp:CheckBoxList>
</ItemTemplate>
</asp:DataList>
But we want the datatable output like this
Thanks in advance!
LINQ can get you partway by allowing you to nest from clauses in queries (simplifying the output a bit):
from DataListItem dli in PollDataList.Items
let z = ... // as in existing code
let y = ...
let v = ...
from checkbox in v.Items
where checkbox.Selected
select new [] { z.Value, checkbox.Value, "" }
Here you will get an output item for each selected checkbox in each poll item (your Answered2). But you also want an output row for the Answered1 value for each poll item, and here LINQ is less helpful. There are two ways you can do this, the quick and ugly way using Enumerable.Concat:
...
let v = ...
from answer in new [] { y.SelectedValue }.Concat(v.Items.Where(r => r.Selected)) // http://msdn.microsoft.com/en-us/library/bb302894.aspx
select new [] { z.Value, answer, "" }
Or the clearer and more general, but longer way, using yield to create an iterator method that gives you an item for all the answers:
from DataListItem item in PollDataList.Items
let z = ...
from answer in PollItemAnswers(item)
...
// outside the method containing the above
IEnumerable<object> PollItemAnswers(PollListItem item)
{
var y = ...; // As in let statements
var v = ...;
yield return y.Value; // return the first answer
foreach (var checkBox in v.Items)
if (checkBox.Selected)
yield return checkBox.Value; // return the other answers
}
And finally, if your final code don't have the items already separated (say, it was posted from a web form), you can use string.Split():
yield return y.Value;
foreach (var answer in value.Split(',')) // "a, b, c" -> "a", " b", " c"
yield return answer.Trim(); // " b" -> "b"
As a final note, watch out for if your answers could contain ,! In general, using something like JSON to safely move value collections around (say from a file or another machine) will save you a lot of grief.
In my user control, there is a GridView.
When I check checkbox in the header of the GridView, I want to select all checkboxs.
My user control can be use more than once in a page.
So I try like this.
My GridView
<asp:GridView ID="GridView" runat="server">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkEach" runat="server" />
</ItemTemplate>
.
.
This is my C# codes
CheckBox chkAll = gvAttachment.Controls[0].Controls[0].FindControl("chkHeader") as CheckBox;
if (chkAll != null)
{
chkAll.Attributes.Add("onclick", "SelectAllChkBox('" + chkAll.ClientID + "','" + GridView.ClientID + "');");
}
This is my javascript
function SelectAllChkBox(value , grid) {
for (var i = 1; i < grid.all.length; i++) {
grid.all[i].checked = value.checked;
}
}
But I got this error.
Unable to get value of the property 'length': object is null or undefined
What is wrong with my Code ?
The parameter grid is the id (a string) of the grid view which means you don't have an "all". length works on the "grid" though. You are not passing an object from c#.
You'll need to find all the check boxes client-side and set checked based on a true or false value.
An array starts from 0:
function SelectAllChkBox(value , grid) {
for (var i = 0; i < grid.all.length; i++) {
grid.all[i].checked = value.checked;
}
}
try this function
function SelectAllChkBox(value , grid) {
var n = document.getElementById("grid").rows.length;
for (var i = 0; i < n; i++) {
grid.all[i].checked = value.checked;
}
}