Popular Posts

Wednesday, 6 July 2011

Passing Multiple Command Arguments In Gridview.

Suppose you have gridview with data and it contains a link which shows Class-Sub Departments.Gridview also contains class-subject id and Practical-Subject Id. Now you want to open new window which contains both id`s ie, class-subject id and Practical-Subject Id. For this purposes, following coding is usefull.


<asp:GridView ID="GridView1" runat="server" SkinID="GridView"
onrowcommand="GridView1_RowCommand" >
<Columns>
                               
<asp:TemplateField HeaderText="Subject" SortExpression="classscheduled">

<ItemTemplate>
<asp:LinkButton ID="Hyper" runat="server" Text='<%# Bind("ClassSubDepartmentName") %>' onclick="Hyper_Click" CommandName="Link" CommandArgument='<%# Eval("ClassSubject")+ ","+ Eval("practicleSubject") %>'></asp:LinkButton>
</ItemTemplate>

</asp:TemplateField>                                                         
                               
</Columns>
<HeaderStyle BackColor="Blue" ForeColor="White" />
</asp:GridView>


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
if (e.CommandName == "Link")
{
ClientScriptManager script = Page.ClientScript;
ScriptManager.RegisterStartupScript(this, GetType(), "show", "openWin('" + commandArgs[0].ToString() + "', '" + commandArgs[1].ToString() + "')", true);
Session["ClassSubId"] = commandArgs[0].ToString();
Session["PracticalSubId"] = commandArgs[1].ToString();
// Here you found both id`s ie, class-subject id and Practical-Subject  //Id and store in Sessions. Use these session`s value in second page.
}


<script type="text/javascript">

function openWin(a ,b)
{
var ClassSubId = a;
var PracticalSubId = b;
              
window.open('YourPageName.aspx?SD=' + ClassSubId + '&PSD=' + PracticalSubId , 'CreateWindow3', '');
}
         
</script>


On second form, retieve both id`s from following code:

int ClassSubId= Convert.ToInt16(Request.QueryString["SD"].ToString());
int PracticalSubId= Convert.ToInt16(Request.QueryString["PSD"].ToString());




Enjoy this

No comments:

Post a Comment