query string on link button - c#

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.

Related

Unable to retrieve date input value C# Web Forms

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];

Display content in Repeater

I must have done so that I can print anything into my Repeater, but it is such that I can not really for it to write the content that I really want to do.
I have written the code like this
forum.aspx
<asp:Repeater ID="RepeaterKommentar" runat="server">
<ItemTemplate>
<div class="kommentarBox">
<%--print content here--%>
</div>
</ItemTemplate>
</asp:Repeater>
forum.aspx.cs
var UsersKommentar = from KommentarA in db.forumkommentars
join BrugereA in db.brugeres on KommentarA.brugerid equals BrugereA.Id
where KommentarA.forumid == getid
orderby KommentarA.Id descending
select new
{
UserName = BrugereA.fornavn + " " + BrugereA.efternavn
};
RepeaterKommentar.DataSource = UsersKommentar;
RepeaterKommentar.DataBind();
The problem, is I'm not entirely sure how to access the object of Enumerable through my data source.
I'm not sure what data exist within your object, but you've bound it to the repeater and called data bind. So all you need to do now is call:
<%# Eval("...") %>
The ... would represent the name of the column, or property.

how to use eval function to get data from string array

I have listview and String array datasource. How can I bind them using eval ?
<telerik:RadListView ID="lvDevSampTableSelection" runat="server" AllowMultiItemSelection="true">
<ItemTemplate>
<p><%# Eval("??") %></p>
</ItemTemplate>
</telerik:RadListView>
here is code behind
ResultDto<String[]> result = client.GetTableNames("IMAPPUSER");
var source = new BindingSource();
source.DataSource = result.Data;
lvDevSampTableSelection.DataSource = source;
lvDevSampTableSelection.DataBind();
I use <%# Container.DataItem %> instead of eval and get data from string to listview
You can't, it needs to evaluate field names from the object that is the datasource (be it columns in datatable or fields in some List). Thus, I think your best option is to create a custom class with a couple of fields, create a List<> from that class and bind to that list.

How to split a string in a datatable and add into new rows using Linq C#?

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.

Dropdown default select value

This is a Dropdown control where I am binding the data, after bind I am putting the select statement. Even though the index is kept to 0 always select comes last like this:
Current output:
india
Auz
US
--select--
Required output:
--select--
india
AUZ
US
My Code
ddlcounty.DataSource = dtNew;
ddlcounty.DataTextField = "Weight";
ddlcounty.DataValueField = "Weight";
ddlcounty.DataBind();
ddlcounty.Items.Add("--Select--");
ddlcounty.SelectedValue = "0";
What is the change required here?
Thanks
You're doing your binding first.
When you get to the part where you are adding your default condition, you're actually adding to the end of the list.
Instead of :-
ddlcounty.Items.Add("--Select--");
Do :-
ddlcounty.Items.Insert(0, new ListItem("--Select--"));
This will insert your default option as the first element of Items.
Announced edit
You won't need :-
ddlcounty.SelectedValue = 0;
.. as if you don't explicitly specify, the first item in a drop down list is automatically selected.
If, however, you want to be explicit about it, you can do the following:-
ddlcounty.Items.Insert(0, new ListItem("--Select--","0"));
ddlcounty.SelectedValue = 0;
Would you please try below way:
Just set AppendDataBoundItems to true and Insert a ListItem as selected, and 'ClearSelection' before selecting item as below.
ddlcounty.AppendDataBoundItems = true;
ddlcounty.DataSource = dtNew;
ddlcounty.DataTextField = "Weight";
ddlcounty.DataValueField = "Weight";
ddlcounty.DataBind();
ddlcounty.ClearSelection();
ddlcounty.Items.Insert(0, new ListItem { Value = "0", Text = "--Select--", Selected = true });
ddlcounty.SelectedValue = "0";
You could also declare the "select one" ListItem declaratively in your aspx page like so
<asp:DropDownList ID="ddUIC" runat="server" AppendDataBoundItems="true" Width="200px" BackColor="White" Font-Size="10px" SelectedValue='<%# Bind("Weight") %>' DataTextField="Weight" DataValueField="Weight" >
<asp:ListItem Text="Select One" Value=""></asp:ListItem>
/asp:DropDownList>
But your AppendDataBoundItems would have to be set to true
And you could still perform your databinding on the backend.

Categories