I'll try to explain what I'm doing the best I can, but I'm pretty new to asp.net so be patient with me.
I have a SqlDataSource which returns a simple select statement based on the WHERE clause using #COURSE_ID
What I want to-do is every time any one of 2 (this will change as it's going to be generated) asp:LinkButtons are pressed, they will change the #COURSEID value which i'd like to associate with the specific button.
Buttons:
<asp:LinkButton ID="LinkButton2" runat="server" onclick="MenuUpdate_Click">Course1</asp:LinkButton>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="MenuUpdate_Click">Course2</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connString %>" SelectCommand="SELECT Chapter.chapterName, Chapter.chapterID
FROM Chapter
WHERE Chapter.courseID = #COURSE_ID
">
C#
protected void MenuUpdate_Click(object sender, EventArgs e)
{
Parameter p = SqlDataSource1.SelectParameters["COURSE_ID"];
SqlDataSource1.SelectParameters.Remove(p);
SqlDataSource1.SelectParameters.Add("COURSE_ID", THIS NEEDS TO BE VALUE ASSOCIATED TO BUTTON);
ListView1.DataBind();
UpdatePanel1.Update();
}
If anyone has any suggestions that'd be great, I've been trying lots of different things all night with no success :(
Thanks
Try setting the linkbutton IDs to contain the course name (I dont think its possible for IDs to start with numbers, so give them IDs "crs" and then the course name). In the click event for the buttons, extract the ID of the sender and take the entire string after "crs". This is the course name of the linkbutton that was clicked.
Also if "COURSE_ID" is the only parameter of the SelectCommand, I would personally clear all the parameters first. It just ensures that you dont have any parameters from elsewhere. Thats just the way I work though - clear everything and reenter anything which shouldnt have been cleared and then enter anything extra.
Regards,
Rich
Related
I have a dropdownlist inside a template column on a obout grid. Currently i am populating the dropdownlist on page load with a sqldatasource. However, i now have to load the dropdownlist dependent on the value of a certain column. For example: If status = 1, i populate the dropdownlist with a list of available options that pertain to status 1.
<obout:Column ID="colStatus" DataField="wf_status_id" Align="center" HeaderText="Status" HeaderAlign="center" Width="130px" Wrap="true" runat="server" AllowGroupBy="true" AllowFilter="true">
<TemplateSettings EditTemplateId="tmpStatusIDEdit" TemplateId="tmpStatusID" />
</obout:Column>
<obout:GridTemplate runat="server" ID="tmpStatusID" >
<Template>
<%# Container.DataItem["Status"]%>
</Template>
</obout:GridTemplate>
<obout:GridTemplate runat="server" ID="tmpStatusIDEdit" ControlID="ddlStatus" ControlPropertyName="value">
<Template>
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" MenuWidth="215" DataSourceID="sdsStatus" DataTextField="wf_status_text" DataValueField="wf_status_id" />
</Template>
</obout:GridTemplate>
public void OnGridRowDataBound(object sender, Obout.Grid.GridRowEventArgs e)
{
if (e.Row.RowType == Obout.Grid.GridRowType.DataRow)
{
DropDownList ddlStatus = new DropDownList();
ddlStatus = (DropDownList)e.Row.FindControl("ddlStatus");
//LOAD DROP DOWN HERE//
}
}
When i attempt to execute this code, it shows that ddlStatus is null everytime. I have a tried a multitude of ways to get this and for some reason cannot seem to get it. Perhaps aother set of eyes or other ideas could help me out. Please let me know what it is i am doing wrong. Thank you ahead of time
UPDATE:DATABOUND EVENT CODE
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200" MenuWidth="215" OnDataBinding="ddlStatus_DataBinding" DataSourceID="sdsStatus" DataTextField="wf_status_text" DataValueField="wf_status_id" />
protected void ddlStatus_DataBinding(object sender, EventArgs e)
{
Obout.Interface.OboutDropDownList ddl = (Obout.Interface.OboutDropDownList)(sender);
string statusID = Eval("wf_status_id").ToString();
}
I added the DataSource because i do not see another way to have the databinding event triggered.
I don't really know anything about obout controls but I have to assume they act very similar to the asp.net controls and have just been extended. With that assumption in mind, I will try and answer your question.
Your RowDataBound event has a few coding issues... for example, I am not sure why you are defining a new DropDownList and then trying to overwrite it with the next line. Also it sounds like the next line is returning null anyways.
First off I suggest not using the DataBound event at the row level. Use the control's DataBinding event as it will localize your code a lot better because you can trigger off the specific control's DataBinding and therefore not have to search for it. If your code changes (markup or codebehind) it is also a lot easier to change as it will not affect other things and there is less room for introducing bugs.
So I would make the following changes to address this:
Change your DropDownList definition to implement DataBinding:
<obout:OboutDropDownList runat="server" ID="ddlStatus" Width="100%" Height="200"
MenuWidth="215" OnDataBinding="ddlStatus_DataBinding" />
Then implement the OnDataBinding event (get rid of your DataBound event if you weren't using it for anything else):
protected void ddlStatus_DataBinding(object sender, System.EventArgs e)
{
// This will point to ddlStatus on the current row that is DataBinding so you
// don't have to search for it.
OboutDropDownList ddl = (OboutDropDownList)(sender);
// Now you can fill the DropDownList and set the default value how ever you like
...
}
You can also see this other question I answered a long time ago to see if it helps as it is doing the same thing but with a Repeater but it is pretty much the same thing as a grid:
Populating DropDownList inside Repeater not working
EDIT: Changed the code to use OboutDropDownList in the DataBinding.
you can find plenty of samples on obout knowledge base and examples. These should help you out: http://www.obout.com/combobox/aspnet_integration_grid.aspx, http://www.obout.com/grid/aspnet_ajax_cascading_comboboxes.aspx
(they refer to comboBox, but you can easily adapt them do a ddl)
I;m new on SO so please be gentle. I searched and didn't find what I was looking for.
My problem is as follows - I am making a game for Windows Store where you must finish sentences based on the animation you see below the sentence. The game will offer many stories and this is my problem. I used the default GridView template to create the MENU for picking your stories. And this is where I am stuck:
How to tell the app using json that when clicked it should open a specific page (the wanted level)?
This is the code that I need to change probably:
void ItemView_ItemClick(object sender, ItemClickEventArgs e)
{
// Navigate to the appropriate destination page, configuring the new page
// by passing required information as a navigation parameter
var itemId = ((SampleDataItem)e.ClickedItem).UniqueId;
this.Frame.Navigate(typeof(ItemDetailPage), itemId);
}
I know that I need to change the last line so that it takes some information from the json file and changes it for a specific page file name from the solution.
I hope that you understand what I mean :(
Use the template field to add a button with your routing argument.
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Action" CommandName="Action"
CommandArgument='<%#Bind("ID ") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Code Behind:
protected void Gridview1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Action")
{
string ID = e.CommandArgument; // Do something with it.
}
I am iterating through an array of strings.
For each string I need it to be presented in a new textbox on the web page.
I have got this working by just having one textbox available, however this worked as a proof of concept. My problem being I am dealing with larger arrays now, with a varying amount of strings.
Is there a method to dynamically create textboxes on the page in relation to how many strings there are?
Here is what I have so far-
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
WebService1 ws = new WebService1();
foreach (string x in ws.mRETURN(CheckBoxList1.SelectedItem.Text))
{
TextBox1.Text = x;
}
}
So textbox one already exists on the page, however I know need it to be dependant on how many strings there are being passed from the webservice, and relate this number to the amount of textboxes needed.
Try this
Markup
<asp:DataList runat="server" ID="repeatedTextBox">
<ItemTemplate>
<asp:TextBox ID="myTextBox" runat="server" Text='<%# Container.DataItem.ToString() %>'></asp:TextBox>
</ItemTemplate>
</asp:DataList>
Codebehind
repeatedTextBox.DataSource = myChangingArrayOfString; //e.g ws.mRETURN(CheckBoxList1.SelectedItem.Text)
repeatedTextBox.DataBind();
How about using a placeholder on the webpage and adding controls(TextBoxes) to it programatically.
<asp:PlaceHolder id="Area1" runat="server"></asp:PlaceHolder></P>
Then on your back end add:
TextBox tbx = new TextBox();
Area1.Controls.Add(tbx);
In a loop you could do that?
I am still learning so its my best suggestion at this stage.
Derived from source: Source
I'm making a webpage. The goal here is to have a file open when you select it from a dropdown list. Here's the trouble code I have right now:
<asp:DropDownList ID="dropdownFiles" runat="server" Height="18px" onselectedindexchanged="File_Opener" Width="380px">
<asp:ListItem>Please choose a file...</asp:ListItem>
</asp:DropDownList>
See that "onselectedindexchanged" part? Well in the codebehind, I have the following function:
public void File_Opener(object sender, EventArgs e)
{
//here's where I would open the files, but it never even hits this function!
}
For some reason, selecting something in the dropdown list never even triggers that function. plsexplain.
I think you need to set the dropdown to autopostback.
It's a bad user experience to have a file open or take an action on a selection, why??? Because I might think I'm selecting a file name and click the wrong one. It's better to have a button to launch whatever process you want to do after the file is selected.
Use the AutoPostBack property:
<asp:DropDownList ID="dropdownFiles" runat="server" Height="18px" onselectedindexchanged="File_Opener" Width="380px" AutoPostBack="True">
<asp:ListItem>Please choose a file...</asp:ListItem>
</asp:DropDownList>
Humor me: try using the proper casing on OnSelectedIndexChanged. See if that makes a difference.
I have a Repeater with a list of Customers. Against each customer there is a delete link button. As part of the linkbutton I want to pass the Customer object to the Command Arguement as follows (where Container.DataItem is the customer object):
<asp:LinkButton ID="lnkDelete"
OnClientClick="return confirmDelete();"
OnClick="Customer_OnDelete"
CommandArgument="<%# Container.DataItem %>"
CommandName="Delete"
runat="server"></asp:LinkButton>
When I do this:
var button = (((LinkButton) sender));
var customer= button.CommandArgument;
button.CommandArguement is a string. I need all the object properties as we are using Nhibernate so everything needs to be set, the ID of the deleted record is not enough. I have seen examples online regarding passing a comma seperated list of values into the command arguement but want to avoid doing that. Is this possible?
Any ideas?
Thanks
In my opinion the best way for this case is:
Get the ID from CommandArgument
Get the Customer by ID
Delete the Customer Entity
Use the Repeater event OnItemCommand. This event contains RepeaterCommandEventArgs. You cant get the CommandArgument this way:
protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int customerID= Convert.ToInt32(e.CommandArgument.ToString());
}
At your asp:LinkButton tag use:
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "ID") %>'
The issue you are running into here is that you repeater has to get translated into HTML. Therefore you are constrained to the limits of what is allowed by the specification in an element attribute.
On the server side CommandArgument will always be a string, so you cannot do what you want as you have it coded.
Now... there are several hacks you could implement to get around this, like the aforementioned CSV, or you could use binary serialization and Base64 encode the result. However, these are all terrible solutions!
What you need is to re-think how you are doing this. I promise there is an easier way.